C# 查找列表c中的所有匹配元素#
我正在努力让这一切顺利进行:C# 查找列表c中的所有匹配元素#,c#,linq,C#,Linq,我正在努力让这一切顺利进行: public class Foo { public int Id { get; set; } public Bar bar {get; set; } } public class Bar { public int Id { get; set;} } 现在,我想在列表中找到与条形图中的Id匹配的所有Foo对象: List<Foo> foos = new List<Foo>(); int matchId = 1; IEnum
public class Foo
{
public int Id { get; set; }
public Bar bar {get; set; }
}
public class Bar
{
public int Id { get; set;}
}
现在,我想在列表中找到与条形图中的Id匹配的所有Foo对象:
List<Foo> foos = new List<Foo>();
int matchId = 1;
IEnumerable<Foo> fooMatches = foos.FindAll(el => el.Bar.Id == matchId);
List foos=new List();
int matchId=1;
IEnumerable fooMatches=foos.FindAll(el=>el.Bar.Id==matchId);
这只是给了我一个列表,其中包含“foos”中的所有元素,以及与Bar中的Id不匹配的元素
非常感谢您的帮助 在以下位置使用
:
IEnumerable<Foo> fooMatches = foos.Where(el => el.Bar.Id == matchId);
IEnumerable fooMatches=foos.Where(el=>el.Bar.Id==matchId);
可能的重复我假设在这些代码行中,为了简洁起见,您跳过了填充foos
的位。因为在你展示的代码中,你在foos
中什么都没有。。。事实上,如果您输入一些示例数据来演示您的问题,可能会有所帮助。您的代码是正确的。确保foos中的所有项目与您的条件不匹配,其中与FindAll
基本相同。我认为他填充foos
的方式在某种程度上是错误的。你是对的,蒂姆:现在Where()和FindAll()都起作用了,问题出在填充“foos”时的其他地方。谢谢你们的帮助。@user172717你们的标签把我弄糊涂了,因为FindAll
根本不是LINQ
,它会为你们生成一个新的列表,但不是LINQ的东西,我也没有仔细阅读。