与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
}
141733 次浏览

如上所述,如果你想要得到4作为结果,你可以这样做:

var nonintersect = array2.Except(array1);

如果你想要真正的非交集(也是1和4),那么这个应该可以做到:

var nonintersect = array1.Except(array2).Union( array2.Except(array1));

这不是性能最好的解决方案,但对于小列表来说,它应该工作得很好。

我想你可能在找Except:

Except操作符生成集合 两个序列之间的差异。它 只返回第一个中的元素 中未出现的序列 第二。您可以选择提供 你自己的相等比较函数。

查看这个链接这个链接,或谷歌获取更多信息。

你可以使用

a.Except(b).Union(b.Except(a));

或者你可以用

var difference = new HashSet(a);
difference.SymmetricExceptWith(b);

这段代码只枚举每个序列一次,并使用Select(x => x)隐藏结果,以获得一个干净的linq风格的扩展方法。由于它使用HashSet<T>,如果散列分布良好,则运行时为O(n + m)。两个列表中的重复元素将被省略。

public static IEnumerable<T> SymmetricExcept<T>(this IEnumerable<T> seq1,
IEnumerable<T> seq2)
{
HashSet<T> hashSet = new HashSet<T>(seq1);
hashSet.SymmetricExceptWith(seq2);
return hashSet.Select(x => x);
}
我不是100%确定你的非相交方法应该做什么(关于集合理论)-它是
B \ A(所有来自B而不在A中出现的东西)?< br > 如果是,那么您应该能够使用Except操作(B.Except(A)).

/// <summary>
/// Given two list, compare and extract differences
/// http://stackoverflow.com/questions/5620266/the-opposite-of-intersect
/// </summary>
public class CompareList
{
/// <summary>
/// Returns list of items that are in initial but not in final list.
/// </summary>
/// <param name="listA"></param>
/// <param name="listB"></param>
/// <returns></returns>
public static IEnumerable<string> NonIntersect(
List<string> initial, List<string> final)
{
//subtracts the content of initial from final
//assumes that final.length < initial.length
return initial.Except(final);
}


/// <summary>
/// Returns the symmetric difference between the two list.
/// http://en.wikipedia.org/wiki/Symmetric_difference
/// </summary>
/// <param name="initial"></param>
/// <param name="final"></param>
/// <returns></returns>
public static IEnumerable<string> SymmetricDifference(
List<string> initial, List<string> final)
{
IEnumerable<string> setA = NonIntersect(final, initial);
IEnumerable<string> setB = NonIntersect(initial, final);
// sum and return the two set.
return setA.Concat(setB);
}
}

array1.NonIntersect (array2);

不相交这样的运算符在Linq中是不存在的你应该做的

Except -> union -> Except

a.except(b).union(b.Except(a));
string left = "411329_SOFT_MAC_GREEN";
string right= "SOFT_MAC_GREEN";


string[] l = left.Split('_');
string[] r = right.Split('_');


string[] distinctLeft = l.Distinct().ToArray();
string[] distinctRight = r.Distinct().ToArray();


var commonWord = l.Except(r, StringComparer.OrdinalIgnoreCase)
string result = String.Join("_",commonWord);
result = "411329"