在列表项中查找列表项

pro*_*don 3 c#

我对这是如何工作有点困惑.

class TestClass
{
    public int ID {get;set;}
    public List<Stuff> StuffList {get; set;}
}
class Stuff
{
    public int ID {get;set;}
    public string Description {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

所以每个TestClass都有一个列表Stuff.我想要做的是找到一个TestClass包含任何Stuff一个ID0

List<TestClass> TestList = RetrieveAllTestLists();
//Pseudocode:
//
// Find all TestClass in TestList that contain a Stuff with ID == 0;
Run Code Online (Sandbox Code Playgroud)

我试过这个,但它不起作用:

List<TestClass> TestList = RetrieveAllTestLists().Where(x=> x.StuffList.Where(y=> y.ID == 0)).ToList();
Run Code Online (Sandbox Code Playgroud)

谁能向我解释我做错了什么?

Zbi*_*iew 5

你可以使用Any:

List<TestClass> TestList = RetrieveAllTestLists().
                           Where(x => x.StuffList.Any(y=> y.ID == 0)).ToList();
Run Code Online (Sandbox Code Playgroud)

Basicaly Where将选择满足条件的所有行(返回的那些行true),但在这个地方你有另一个Where.如果有任何行满足给定条件,Any将返回true.