使用的IEqualityComparer的联盟

问题描述:

我只是想从两个列表删除重复项,并将它们组合成一个列表。我还需要能够定义一个重复是什么。我由器的columnIndex属性定义一个重复的,如果它们是相同的,它们是重复的。这里是我采取的方法:

I simply want to remove duplicates from two lists and combine them into one list. I also need to be able to define what a duplicate is. I define a duplicate by the ColumnIndex property, if they are the same, they are duplicates. Here is the approach I took:

我发现了如何编写内嵌comparers的,你需要它们才有一次代码段随机场合一记漂亮的例子。 p>

I found a nifty example of how to write inline comparers for the random occassions where you need em only once in a code segment.

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);
    }
}



然后我有我的两个列表,并尝试他们工会与比较器。

Then I just have my two lists, and attempt a union on them with the comparer.

            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);



不过的结果集为空。

The result set however is null.

我在哪里误入歧途?
我已经证实,两个列表有相同columnIndex属性栏。

Where am I going astray? I have already confirmed that the two lists have columns with equal ColumnIndex properties.

我刚刚运行!在测试代码集....和它的作品

I've just run your code on a test set.... and it works!

    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);
    }



所以...也许回去检查你的测试数据 - 或者运行与其他一些测试数据

So... maybe go back and check your test data - or run it with some other test data?

毕竟 - 一个联盟是空的 - 这意味着,无论你输入列表也为空

After all - for a Union to be empty - that suggests that both your input lists are also empty?