使用 LINQ 获取一个 List<> 中的项目,而不是另一个 List<> 中的项目 [英] Use LINQ to get items in one List&lt;&gt;, that are not in another List&lt;&gt;

查看:25
本文介绍了使用 LINQ 获取一个 List<> 中的项目,而不是另一个 List<> 中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为有一个简单的 LINQ 查询可以做到这一点,我只是不确定如何.

给定这段代码:

class 程序{静态无效主(字符串 [] args){列表<人>peopleList1 = new List();peopleList1.Add(new Person() { ID = 1 });peopleList1.Add(new Person() { ID = 2 });peopleList1.Add(new Person() { ID = 3 });列表<人>peopleList2 = new List();peopleList2.Add(new Person() { ID = 1 });peopleList2.Add(new Person() { ID = 2 });peopleList2.Add(new Person() { ID = 3 });peopleList2.Add(new Person() { ID = 4 });peopleList2.Add(new Person() { ID = 5 });}}班级人物{公共 int ID { 获取;放;}}

我想执行一个 LINQ 查询,以提供 peopleList2 中所有不在 peopleList1 中的人.

这个例子应该给我两个人(ID = 4 & ID = 5)

解决方案

这可以使用以下 LINQ 表达式解决:

var result = peopleList2.Where(p => !peopleList1.Any(p2 => p2.ID == p.ID));

通过 LINQ 表达这一点的另一种方式,一些开发人员发现这种方式更具可读性:

var result = peopleList2.Where(p => peopleList1.All(p2 => p2.ID != p.ID));

<块引用>

警告: 如评论中所述,这些方法要求 O(n*m) 操作.这可能没问题,但可能会带来性能问题,尤其是在数据集非常大的情况下.如果这不能满足您的性能要求,您可能需要评估其他选项.但是,由于所述要求是针对 LINQ 中的解决方案,因此这里不探讨这些选项.与往常一样,根据您的项目可能具有的性能要求评估任何方法.

I would assume there's a simple LINQ query to do this, I'm just not exactly sure how.

Given this piece of code:

class Program
{
    static void Main(string[] args)
    {
        List<Person> peopleList1 = new List<Person>();
        peopleList1.Add(new Person() { ID = 1 });
        peopleList1.Add(new Person() { ID = 2 });
        peopleList1.Add(new Person() { ID = 3 });

        List<Person> peopleList2 = new List<Person>();
        peopleList2.Add(new Person() { ID = 1 });
        peopleList2.Add(new Person() { ID = 2 });
        peopleList2.Add(new Person() { ID = 3 });
        peopleList2.Add(new Person() { ID = 4 });
        peopleList2.Add(new Person() { ID = 5 });
    }
}

class Person
{
    public int ID { get; set; }
}

I would like to perform a LINQ query to give me all of the people in peopleList2 that are not in peopleList1.

This example should give me two people (ID = 4 & ID = 5)

解决方案

This can be addressed using the following LINQ expression:

var result = peopleList2.Where(p => !peopleList1.Any(p2 => p2.ID == p.ID));

An alternate way of expressing this via LINQ, which some developers find more readable:

var result = peopleList2.Where(p => peopleList1.All(p2 => p2.ID != p.ID));

Warning: As noted in the comments, these approaches mandate an O(n*m) operation. That may be fine, but could introduce performance issues, and especially if the data set is quite large. If this doesn't satisfy your performance requirements, you may need to evaluate other options. Since the stated requirement is for a solution in LINQ, however, those options aren't explored here. As always, evaluate any approach against the performance requirements your project might have.

这篇关于使用 LINQ 获取一个 List<> 中的项目,而不是另一个 List<> 中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆