1

I have a data table that looks like this (Not my table can't change the data)

Code     Version    Description
AAA      0.0.0.0    Test of AAA
AAA      0.0.0.1    Test of AAA
BBB      0.0.0.0    Test of BBB
CCC      0.0.0.0    Test of CCC
CCC      0.0.0.1    Test of CCC

I want to return a list of unique "Code" values only.
So my desired result will be:

AAA   Test of AAA
BBB   Test of BBB
CCC   Test of CCC

I've created a comparer class:

public class MyComparer : IEqualityComparer<MY_DATA_TABLE>
{
    public bool Equals(MY_DATA_TABLE x, MY_DATA_TABLE y)
    {
        return x.CODE == y.CODE;
    }

    public int GetHashCode(MY_DATA_TABLE obj)
    {
        return obj.GetHashCode();
    }
}

and in my code I have:

var mapCodes = (from mtc in GetAllData() select mtc)
                    .Distinct(new MyComparer ())
                    .ToList();

However it is still returning the entire contents back.

How can I achieve getting a distinct list by property?

Daniel Corzo
  • 1,055
  • 2
  • 19
  • 32
John Doe
  • 3,053
  • 17
  • 48
  • 75

3 Answers3

5

Another option is to do a group by and take the first value in each group if Version doesn't matter.

var mapCodes = (from mtc in GetAllData()
                group mtc by mtc.Code into grp
                select grp.First()).ToList();
juharr
  • 31,741
  • 4
  • 58
  • 93
2

You can use MoreLinq's DistinctBy

 var mapCodes = (from mtc in GetAllData()
                 select mtc).DistinctBy(x=>x.Code).ToList();

Or just:

var mapCodes = GetAllData().DistinctBy(x=>x.Code).ToList();

Or you can correct the GetHashCode method as mentioned in the comments

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
The One
  • 4,560
  • 5
  • 36
  • 52
1

Rewrite your GetHashCode() function:

public int GetHashCode(MY_DATA_TABLE obj)
{
    return obj.CODE.GetHashCode();
}

Rule is, that both Equals and GetHashCode() should check the same properties, and you checking just Code in Equals() and whiole object in GetHashCode()

Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49