使用LINQ获得两个列表之间的差异 [英] Get the difference between two lists using LINQ

查看:66
本文介绍了使用LINQ获得两个列表之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表,我需要比较它们,并且只返回不在两个列表中的项目列表.

I have two lists and I need to compare them and only return a List of Items not in both.

var listOfIds = new List<int> {1,2,4};

var persons = new ObservableCollection<Person>
{
    new Person {Id = 1, Name = "Person 1"},
    new Person {Id = 2, Name = "Person 2"},
    new Person {Id = 3, Name = "Person 3"},
    new Person {Id = 4, Name = "Person 4"}
};

在此示例中,将为new Person {Id = 3, Name = "Person 3"}. 最好使用Linq解决方案.

In this example new Person {Id = 3, Name = "Person 3"} would be the result. A Linq solution would be preferred.

推荐答案

不适合您

var listOfIds = new List<int> {1,2,4};

var query = from item in persons 
            where !listOfIds .Contains( item.id )
            select item;

您可以检查更多详细信息: SQL to LINQ(案例7-过滤器使用IN和NOT IN子句获取数据)

You can check for more detail : SQL to LINQ ( Case 7 - Filter data by using IN and NOT IN clause)

这篇关于使用LINQ获得两个列表之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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