25

我想得到 c# 中两组整数之间的区别。给定 s1 和 s2 我想返回那些在 s1 中而不是在 s2 中的整数。我可以做一些事情,例如:

    List<int> s1 = new List<int>();
    List<int> s2 = new List<int>();

    foreach (int i in s1)
    {
        if (s1.Contains(i))
        {
            //
        }
        else
        {
            //
        }
    }

但我想知道是否有人可以指出任何更清洁的东西。我想做一些事情,比如

List<int> omitted = s1.Difference(s2);

不确定是否存在任何人都可以指出的现有方法或 LINQ 构造?谢谢你。

4

6 回答 6

33
IEnumerable<T> a, b;

var added = a.Except(b);
var removed = b.Except(a);
于 2009-04-30T09:54:10.147 回答
30

我想你想要HashSet.Except。也就是不使用Lists,使用HashSets,然后操作就可以了。如果您所代表的确实是一个“集合”,那么这是一种更好的类型。(如果您已经有一个列表,您可以从中创建一个“新 HashSet”。)

于 2009-04-30T09:45:49.510 回答
3

另一个有用的 API,得到对称的差异:

HashSet.SymmetricExceptWith()

于 2018-09-17T22:49:17.727 回答
2
List<int> s1 = new List<int>();
List<int> s2 = new List<int>();

return sl.FindAll( i => !s2.Contains(i) )
于 2009-04-30T09:49:48.893 回答
1
来自 s1 中的 x
在哪里 !s2.包含(x)
选择 x
于 2009-04-30T09:49:42.573 回答
0

当您需要找到两个 IEnumerable 之间的无序差异时,这两种扩展方法可能会派上用场(它或多或少与 leppie wrapper 对扩展方法给出的答案相同):

public class EnumerableDifferences<T>
{
    public IEnumerable<T> Added { get; }
    public IEnumerable<T> Removed { get; }

    public EnumerableDifferences(IEnumerable<T> added, IEnumerable<T> removed)
    {
        Added = added;
        Removed = removed;
    }
}

public static class EnumerableExtensions
{
    public static HashSet<TSource> ToHashSet<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
    {
        return new HashSet<TSource>(source, comparer);
    }

    public static IEnumerable<TSource> ExceptBy<TSource, TKey>(this IEnumerable<TSource> first, IEnumerable<TSource> second, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> keyComparer = null)
    {
        return first
            .ExceptBy(keySelector, second.Select(keySelector), keyComparer);
    }

    public static IEnumerable<TSource> ExceptBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEnumerable<TKey> keys, IEqualityComparer<TKey> keyComparer = null)
    {
        var secondKeys = keys.ToHashSet(keyComparer);

        foreach (var firstItem in source)
        {
            var firstItemKey = keySelector(firstItem);

            if (!secondKeys.Contains(firstItemKey))
            {
                yield return firstItem;
            }
        }
    }

    public static EnumerableDifferences<TSource> DifferencesBy<TSource, TKey>(this IEnumerable<TSource> first, IEnumerable<TSource> second, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> keyComparer = null)
    {
        keyComparer = keyComparer ?? EqualityComparer<TKey>.Default;

        var removed = first.ExceptBy(second, keySelector, keyComparer);
        var added = second.ExceptBy(first, keySelector, keyComparer);

        var result = new EnumerableDifferences<TSource>(added, removed);

        return result;
    }

    public static EnumerableDifferences<TSource> Differences<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer = null)
    {
        return first
            .DifferencesBy(second, x => x, comparer);
    }
}

public static class Program
{
    public static void Main(params string[] args)
    {
        var l1 = new[] { 'a', 'b', 'c' };
        var l2 = new[] { 'a', 'd', 'c' };

        var result = l1.Differences(l2);

        Console.ReadKey();
    }
}
于 2017-12-28T15:03:08.167 回答