我有一个名为mainList的列表.mainList中的每个项都包含另一个名为detailList的列表.
我想从mainList中选择detailList中的属性评估为true的项目.
我希望能起作用:
var list = mainList.Where(x => x.detailList.Where(y => y.property == true));
Run Code Online (Sandbox Code Playgroud)
这不起作用,它无法将detailList转换为bool.
所以我的问题是如何在mainList中选择项目,其中该项目在detailList中具有有效属性.
如果所有项目都必须为真:
var list = mainList.Where(x => x.detailList.All(y => y.property));
Run Code Online (Sandbox Code Playgroud)
如果至少一个
var list = mainList.Where(x => x.detailList.Any(y => y.property));
Run Code Online (Sandbox Code Playgroud)