问题描述
我想找到2个系列之间的区别.所以我在LINQ语句中使用Except
.但是Except
似乎仅在第一个集合长于第二个集合时才起作用.例如,即使两个集合不同,它也不会返回任何结果.
I want to find a difference between 2 series. So I am using Except
in the LINQ statement. But Except
seems to work only when the first collection is longer than the second. For example this will not return any result, even though the 2 collections are different.
double[] numbers1 = { 2.0, 2.1, 2.2, 2.3, 2.4, 2.5 };
double[] numbers2 = { 2.2 };
IEnumerable<double> onlyInFirstSet = numbers2.Except(numbers1);
任何人都可以确认是否是这种情况吗?如果是这样,我必须在编写查询之前检查集合的长度,因为我不知道哪个集合在编译时会更大.
Can anyone confirm if this is the case? If so, do I have to check the collection lengths before I write the query, because I do not know which collection will be bigger at compile time.
编辑
我想我不清楚我的问题.我不在乎哪个集合包含什么.我只想找到2个收藏夹之间的区别.我该怎么办?
I think I was not clear in my question. I do not care which collection contains what. I just want to find difference between 2 collections. How can I do this?
推荐答案
来自 101个LINQ样本:
int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
int[] numbersB = { 1, 3, 5, 7, 8 };
IEnumerable<int> aOnlyNumbers = numbersA.Except(numbersB);
Console.WriteLine("Numbers in first array but not second array:");
foreach (var n in aOnlyNumbers)
{
Console.WriteLine(n);
}
结果
第一个数组中的数字,而不是第二个数组中的数字:02个469
Numbers in first array but not second array:02469
这篇关于LINQ Except如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!