获取的两个列表使用LINQ差异 [英] Get Difference between two list using linq
本文介绍了获取的两个列表使用LINQ差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个列表,我需要对它们进行比较,并只返回的项目列表不同时。
I got 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"}
};
在这个例子中新的Person {n = 3,名称=3人}
会是这个结果。
LINQ的解决方案将是preferred。
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中的LINQ(案例7 - 过滤器通过使用和NOT IN子句的数据)
You can check for more detail : SQL to LINQ ( Case 7 - Filter data by using IN and NOT IN clause)
这篇关于获取的两个列表使用LINQ差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文