gpt4 book ai didi

c# - 在列表项中查找列表项

转载 作者:行者123 更新时间:2023-11-30 15:00:39 24 4
gpt4 key购买 nike

我对它的工作原理有点困惑。

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;}
}

因此每个 TestClass 中都有一个 Stuff 列表。我想要做的是找到一个 TestClass,其中包含 ID0

的任何 Stuff
List<TestClass> TestList = RetrieveAllTestLists();
//Pseudocode:
//
// Find all TestClass in TestList that contain a Stuff with ID == 0;

我已经试过了,但没用:

List<TestClass> TestList = RetrieveAllTestLists().Where(x=> x.StuffList.Where(y=> y.ID == 0)).ToList();

谁能告诉我我做错了什么?

最佳答案

您可以使用Any:

List<TestClass> TestList = RetrieveAllTestLists().
Where(x => x.StuffList.Any(y=> y.ID == 0)).ToList();

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

关于c# - 在列表项中查找列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15253977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com