与 Intersect() 相反
Intersect 可用于查找两个集合之间的匹配项,如下所示:
// Assign two arrays.
int[] array1 = { 1, 2, 3 };
int[] array2 = { 2, 3, 4 };
// Call Intersect extension method.
var intersect = array1.Intersect(array2);
// Write intersection to screen.
foreach (int value in intersect)
{
Console.WriteLine(value); // Output: 2, 3
}
但是我想要实现的目标却相反,我想列出一个集合中缺少另一个集合的项目:
// Assign two arrays.
int[] array1 = { 1, 2, 3 };
int[] array2 = { 2, 3, 4 };
// Call "NonIntersect" extension method.
var intersect = array1.NonIntersect(array2); // I've made up the NonIntersect method
// Write intersection to screen.
foreach (int value in intersect)
{
Console.WriteLine(value); // Output: 4
}
Intersect can be used to find matches between two collections, like so:
// Assign two arrays.
int[] array1 = { 1, 2, 3 };
int[] array2 = { 2, 3, 4 };
// Call Intersect extension method.
var intersect = array1.Intersect(array2);
// Write intersection to screen.
foreach (int value in intersect)
{
Console.WriteLine(value); // Output: 2, 3
}
However what I'd like to achieve is the opposite, I'd like to list items from one collection that are missing from the other:
// Assign two arrays.
int[] array1 = { 1, 2, 3 };
int[] array2 = { 2, 3, 4 };
// Call "NonIntersect" extension method.
var intersect = array1.NonIntersect(array2); // I've made up the NonIntersect method
// Write intersection to screen.
foreach (int value in intersect)
{
Console.WriteLine(value); // Output: 4
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如前所述,如果您想得到 4 作为结果,您可以这样做:
如果您想要真正的非交集(也包括 1 和 4),那么这应该可以解决问题:
这不会是性能最高的解决方案,但对于小列表,它应该可以正常工作。
As stated, if you want to get 4 as the result, you can do like this:
If you want the real non-intersection (also both 1 and 4), then this should do the trick:
This will not be the most performant solution, but for small lists it should work just fine.
您可以使用
或者您可以使用
You can use
Or you can use
此代码仅枚举每个序列一次,并使用
Select(x => x)
隐藏结果以获得干净的 Linq 风格的扩展方法。由于它使用HashSet
,如果哈希分布良好,其运行时间为O(n + m)
。任一列表中的重复元素都会被省略。This code enumerates each sequence only once and uses
Select(x => x)
to hide the result to get a clean Linq-style extension method. Since it usesHashSet<T>
its runtime isO(n + m)
if the hashes are well distributed. Duplicate elements in either list are omitted.我想您可能正在寻找
Except
:查看此链接,此链接,或 Google,了解更多信息。
I think you might be looking for
Except
:Check out this link, this link, or Google, for more information.
array1.NonIntersect(array2);
Linq 中不存在非相交这样的运算符,您应该这样做,
除了 ->联盟->除了
array1.NonIntersect(array2);
Nonintersect such operator is not present in Linq you should do
except -> union -> except
我不是 100% 确定你的 NonIntersect 方法应该做什么(关于集合论) - 是吗
B \ A(B 中未出现在 A 中的所有内容)?
如果是,那么您应该能够使用 except 操作 (B.Except(A))。
I'm not 100% sure what your NonIntersect method is supposed to do (regarding set theory) - is it
B \ A (everything from B that does not occur in A)?
If yes, then you should be able to use the Except operation (B.Except(A)).