【问题标题】:Filter List By items in Array (LINQ)按数组中的项目过滤列表(LINQ)
【发布时间】:2016-04-04 18:34:33
【问题描述】:

我有Fruit的列表:

List<Fruit>

其中Fruit 具有以下属性/字段:

id
name
color

给定一个整数数组:

int[] ids = new [] {1,2,8};

如何过滤我的列表,以便排除 id 在数组中的水果?

【问题讨论】:

标签: c# linq


【解决方案1】:
var l = new List<Fruit>();
var exceptions = new int[] {1,2,8};
var filtered = l.Where(x=> !exceptions.Contains(x.id));

请注意,这将返回一个新过滤的IEnumerable&lt;Fruit&gt;;它不会从原始列表中删除项目。要真正从列表中删除它们,您可以改用:

l.RemoveAll(x => exceptions.Contains(x.id));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 2015-12-13
    相关资源
    最近更新 更多