mardi 26 septembre 2017

Issue converting nested loops to LINQ

I'm struggling to convert the following nested loops, with conditions to a more readable LINQ statement.

IEnumerable<PropertyInfo> testClassPageObjects = FindPageObjects();
T newControl = default(T);

Parallel.ForEach(testClassPageObjects, (pageObject,state) =>
{              
    Type pageObjectType = pageObject.PropertyType;
    var pageObjectControls = pageObjectType.GetFields();

    foreach (var control in pageObjectControls.Where(control => control.FieldType == typeof(T)))
    {
        if (control.Name == fieldNameNoSpaces)
        {
            var findsByAttribute = (FindsByAttribute) control.GetCustomAttribute(typeof(FindsByAttribute));
            newControl = (T) control
                .FieldType
                .GetConstructor(new[] {typeof(IWebDriver), typeof(By)})
                .Invoke(new object[] {driver, findsByAttribute.Locator});

            state.Break();
        }
    }              
});

Everything before and including the if statement I would like in a LINQ statement if possible. The statement should return the first match. The use of Parallel.ForEach and foreach need not be relevant in this case.

Many thanks,





Aucun commentaire:

Enregistrer un commentaire