我有两个列表,我需要对它们进行比较,并只返回的项目列表不同时。
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)