我努力將下面的嵌套循環轉換爲更易讀的LINQ語句。將嵌套循環轉換爲LINQ的問題
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();
}
}
});
之前的所有內容,包括if語句我希望在LINQ語句中如果可能的話。該聲明應返回第一場比賽。在這種情況下,使用Parallel.ForEach和foreach不必相關。
非常感謝,
您可以開始在您的Where()條件中添加&& control.Name == fieldNameNoSpaces'。我不知道爲什麼你在一個地方有一半的條件,其餘的在另一個地方。 – itsme86
你試過了什麼?這是*不是*外包網站 – barakcaf