gpt4 book ai didi

c# - C# 查找列表中所有匹配的元素

转载 作者:行者123 更新时间:2023-12-03 03:03:46 24 4
gpt4 key购买 nike

我正在努力让它发挥作用:

public class Foo
{
public int Id { get; set; }
public Bar bar {get; set; }
}

public class Bar
{
public int Id { get; set;}
}

现在我想在列表中找到与 Bar 中的 Id 匹配的 Foo 的所有对象:

List<Foo> foos = new List<Foo>();
int matchId = 1;
IEnumerable<Foo> fooMatches = foos.FindAll(el => el.Bar.Id == matchId);

这只是给了我一个列表,其中包含“foos”中的所有元素,以及与 Bar 中的 Id 不匹配的元素

非常感谢任何帮助。

最佳答案

使用地点:

IEnumerable<Foo> fooMatches = foos.Where(el => el.Bar.Id == matchId);

关于c# - C# 查找列表中所有匹配的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20125048/

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