Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using IEqualityComparer for Union

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:

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.

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

like image 457
Matthew Cox Avatar asked May 11 '11 19:05

Matthew Cox


People also ask

How to use Union in C#?

The Union operator is Not Supported in C# & VB.Net Query syntax. However, you can use Union method on query variable or wrap whole query into brackets and then call Union().

What is C# Union?

The LINQ Union Method in C# is used to combine the multiple data sources into one data source by removing the duplicate elements. There are two overloaded versions available for the LINQ Union Method as shown below.


1 Answers

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?

like image 131
Stuart Avatar answered Nov 02 '22 23:11

Stuart