【发布时间】:2014-10-20 23:34:54
【问题描述】:
我已将以下 foreach 循环转换为 LINQ 语句。
foreach (Plant plant in Plant.ListPlants)
{
foreach (Program program in plant.GetAllPrograms())
{
if (program.GetProfitCenter() != null)
{
foreach (CostCenter costCenter in program.GetProfitCenter().GetAllCostCenters())
{
foreach (Account account in costCenter.GetAccounts())
{
if (account.Code == Code)//Code is a parameter
{
return account;
}
}
}
}
}
}
只要不存在此类帐户代码,结果也应返回 null。 请帮我为上述循环构建 LINQ。
【问题讨论】:
-
我试过这个“ Plant.ListPlants.First(plant => plant.GetAllPrograms().First(program => program.GetProfitCenter() != null && program.GetProfitCenter().GetAllCostCenters( ).First(costCenter => costCenter.GetAccounts().First(account => account.Code == Code))));"但它显示无法将 Account 隐式转换为 bool
-
有点难读,但我认为您需要在 costCenter => costCenter.GetAccounts().First(account => account.Code == Code) 周围添加 ()