使用IEqualityComparer for Union

时间:2011-05-11 19:26:35

标签: c# linq iequalitycomparer

我只是想从两个列表中删除重复项并将它们合并到一个列表中。我还需要能够定义重复的内容。我通过ColumnIndex属性定义副本,如果它们相同,则它们是重复的。这是我采取的方法:

我找到了一个很好的例子,说明如何为代码段中只需要一次的随机场所编写内联比较器。

public class InlineComparer<T> : IEqualityComparer<T>
{
    private readonly Func<T, T, bool> getEquals;
    private readonly Func<T, int> getHashCode;

    public InlineComparer(Func<T, T, bool> equals, Func<T, int> hashCode)
    {
        getEquals = equals;
        getHashCode = hashCode;
    }

    public bool Equals(T x, T y)
    {
        return getEquals(x, y);
    }

    public int GetHashCode(T obj)
    {
        return getHashCode(obj);
    }
}

然后我只有两个列表,并尝试使用比较器与它们联合。

            var formatIssues = issues.Where(i => i.IsFormatError == true);
            var groupIssues = issues.Where(i => i.IsGroupError == true);

            var dupComparer = new InlineComparer<Issue>((i1, i2) => i1.ColumnInfo.ColumnIndex == i2.ColumnInfo.ColumnIndex, 
            i => i.ColumnInfo.ColumnIndex);

            var filteredIssues = groupIssues.Union(formatIssues, dupComparer);

但结果集为空。

我哪里误入歧途? 我已经确认这两个列表的列具有相同的ColumnIndex属性。

3 个答案:

答案 0 :(得分:13)

我只是在测试集上运行你的代码....它可以工作!

    public class InlineComparer<T> : IEqualityComparer<T>
    {
        private readonly Func<T, T, bool> getEquals;
        private readonly Func<T, int> getHashCode;

        public InlineComparer(Func<T, T, bool> equals, Func<T, int> hashCode)
        {
            getEquals = equals;
            getHashCode = hashCode;
        }

        public bool Equals(T x, T y)
        {
            return getEquals(x, y);
        }

        public int GetHashCode(T obj)
        {
            return getHashCode(obj);
        }
    }

    class TestClass
    {
        public string S { get; set; }
    }

    [TestMethod]
    public void testThis()
    {
        var l1 = new List<TestClass>()
                     {
                         new TestClass() {S = "one"},
                         new TestClass() {S = "two"},
                     };
        var l2 = new List<TestClass>()
                     {
                         new TestClass() {S = "three"},
                         new TestClass() {S = "two"},
                     };

        var dupComparer = new InlineComparer<TestClass>((i1, i2) => i1.S == i2.S, i => i.S.GetHashCode());

        var unionList = l1.Union(l2, dupComparer);

        Assert.AreEqual(3, unionList);
    }

所以...也许回去查看你的测试数据 - 或者用其他一些测试数据运行它?

毕竟 - 对于一个联盟是空的 - 这表明你的输入列表也都是空的?

答案 1 :(得分:5)

一种稍微简单的方式:

  • 确实保留了原始订单
  • 它忽略了发现它们的欺骗

使用链接扩展方法:

   formatIssues.Union(groupIssues).DistinctBy(x => x.ColumnIndex)

这是来自MoreLinq

DistinctBy lambda方法
public static IEnumerable<TSource> DistinctBy<TSource, TKey>
     (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
    HashSet<TKey> knownKeys = new HashSet<TKey>();
    foreach (TSource element in source)
    {
        if (knownKeys.Add(keySelector(element)))
        {
            yield return element;
        }
    }
}

答案 2 :(得分:3)

Linq Except方法不适合你吗?

var formatIssues = issues.Where(i => i.IsFormatError == true);
var groupIssues = issues.Where(i => i.IsGroupError == true);

var dupeIssues = issues.Where(i => issues.Except(new List<Issue> {i})
                                        .Any(x => x.ColumnIndex == i.ColumnIndex));

var filteredIssues = formatIssues.Union(groupIssues).Except(dupeIssues);