返回在C#列表中的所有条目匹配的int

返回在C#列表中的所有条目匹配的int

问题描述:

我有具有以下的列表:返回在C#列表中的所有条目匹配的int

class GetMyList 
{ 
    public int searchInt { get; set; } 
    public string Name { get; set; } 
    public string Type { get; set; } 
    public string Retype { get; set; } 
    public string Time { get; set; } 
    public string City { get; set; } 
    public DateTime dateHere { get; set; } 
} 

该列表可能具有多个条目的每个INT(即searchInt 1可以有一个条目为4/11/2017和另一个为4/12/2017)。

如何在上面的示例中返回与1的searchInt匹配的所有条目?最好让我可以访问dateHere字段,但如果知道其他问题,很高兴知道其他字段。

+2

你试过了:'list.Where(entry => entry.searchInt == 1)'? –

你可以简单地使用Linq。

var filter = collection.Where(search => search.searchInt == 1); 

Where将在谓词精华比赛,所以在这种情况下,计算结果为true在我发言的所有项目,将被退回。

+0

我对LINQ不熟悉(听起来像我需要这样做),但是我能够根据foreach循环+这个来适应我想要的代码。感谢你们的帮助! –