我需要帮助将以下代码转换为LINQ语句:
var defaultAcordConstructionType = new AcordConstructionType();
foreach (var constructionType in ds.GetConstructionTypes())
{
defaultAcordConstructionType = constructionType.AcordConstructionTypes.FirstOrDefault(a => a.IsDefault.HasValue && a.IsDefault.Value);
if (defaultAcordConstructionType != null)
{
break;
}
}
如果不清楚:
任何帮助将不胜感激。谢谢。
答案 0 :(得分:3)
您可以使用SelectMany
来“展平”一对多收藏品:
defaultAcordConstructionType =
ds.GetConstructionTypes()
.SelectMany(constructionType => constructionType.AcordConstructionTypes)
.FirstOrDefault(a => a.IsDefault.HasValue && a.IsDefault.Value)