说我有,
List<ExampleType> example;
它已经填充了 10 个 ExampleType 对象。
ExampleType 看起来像这样:
class ExampleType
{
public string ID;
public string name;
//etc.
//Some other members..
}
现在,我将如何遍历列表 example
,并仅查询具有名称的 ExampleType
对象,例如Peter 并将其存储在另一个列表中,例如:
List<ExampleType> peterExamples = example. //some query functionality that I can't find
我试过 example.AsQueryable
。但无法让它工作。我怀疑我可能需要一些 LINQ 来查询列表?
最佳答案
你可以通过 name == "Peter"
获取所有 ExampleType
元素,如下所示:
var peterExamples = example.Where(e => e.name == "Peter");
这将返回一个 IEnumerable
,如果您需要一个 List
,您可以通过调用 ToList()
将其转换为一个列表。
var peterList = peterExamples.ToList();
关于c# - 如何 "query"列表<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21540695/