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

我正在尝试使它起作用:

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不匹配

非常感谢您的帮助。

参考方案

使用Where

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

LINQ RemoveAll代替循环 - c#

我有两个for循环,用于从列表中删除项目。我正在为这些循环寻找等效的LINQ语句for (Int32 i = points.Count - 1; i >= 0; i--) { for (Int32 j = touchingRects.Count - 1; j >= 0; j--) { if (touchingRects[j].HitTest(po…

Linq Any()的哪种使用效率更高? - c#

我有一个Linq查询,如下所示:return this._alarmObjectAlarmViolationList .Where(row => row.ObjectId == subId) .Where(row => row.AlarmInternalId == "WECO #1 (StdDev > UCL)") .W…

Java中的OrderByDecending(LINQ)等效项 - java

嗨,我是一名使用Java的C#开发人员。问题很简单:我如何才能将下面的c#代码写入Java并仍能正常工作:myCoffeeList.OrderByDescending(x => x.Name?.ToLower()?.Trim() == sender.Text.ToLower()?.Trim())); 我的sender.Text基本上是一个文本框。文本的…

LINQ to SQL中的“无法识别的表达式节点数组索引linq”异常 - c#

这是我的LINQ,其中我将DateTime类型的字段与当前日期进行了比较- var srs = (from s in dcDistrict.ScheduledReportStatus where s.ReportConfigId.Equals(ConfigId) && s.Status.HasValue && s.Status…

.Net LINQ-使用其他字典过滤字典 - c#

我有两个相同类型的字典,A和B。Dictionary<string, IEnumerable<object>> 我使用对象来表示具有属性“ Id”的复杂类型。我正在寻找A中具有B中存在的对象(使用Id)但在另一个键下的所有项目。基本上是要判断对象是否已移动键。 A是新字典,B是旧字典。有没有使用LINQ完成此操作的合理方法?我希望结果…