1

I am having list of Item.

class Item{
      public int Year { get; set; }
      public int QuarterIndex { get; set; }
}

How to convert List to distinct List?

Source:

List<Item> items = new List<Item>(){
 new Item(){ Year = 2013, QuarterIndex = 1},
 new Item(){ Year = 2013, QuarterIndex = 2},
 new Item(){ Year = 2013, QuarterIndex = 3},
 new Item(){ Year = 2013, QuarterIndex = 1}
};

Result:

target = new List<Item>(){
 new Item(){ Year = 2013, QuarterIndex = 1},
 new Item(){ Year = 2013, QuarterIndex = 2},
 new Item(){ Year = 2013, QuarterIndex = 3}
};
1
  • 1
    You should: 1) Make Item implement IEquatable<Item>; 2) use Distinct() Commented Jul 21, 2014 at 11:12

2 Answers 2

10

Here's a simple but potentially less efficient way which works without modifying the class itself:

items = items.GroupBy(i => new { i.Year, i.QuarterIndex })
    .Select(g => g.First())
    .ToList();

Another way is to implement a custom IEqualityComparer<Item> which you can use for Distinct (and other methods in Enumerable class):

public class ItemComparer : IEqualityComparer<Item>
{
    public bool Equals(Item lhs, Item rhs)
    {
        if(lhs == null || rhs == null) return false;
        return lhs.Year == rhs.Year && lhs.QuarterIndex == rhs.QuarterIndex;
    }

    public int GetHashCode(Item item)
    {
        if(item == null) return 0;
        unchecked
        {
            int hash = 23;
            hash = (hash * 31) + item.Year;
            hash = (hash * 31) + item.QuarterIndex;
            return hash;
        }
    }
}

Now this works:

items = items.Distinct(new ItemComparer()).ToList();

If you can/want to modify the original class you can override Equals + GetHashCode:

public class Item
{
    public int Year { get; set; }
    public int QuarterIndex { get; set; }

    public override bool Equals(object otherItem)
    {
        Item other = otherItem as Item;
        if (other == null) return false;
        return this.Equals(other);
    }

    public bool Equals(Item otherItem)
    {
        if(otherItem == null) return false;
        return Year == otherItem.Year && QuarterIndex == otherItem.QuarterIndex;
    }

    public override int GetHashCode()
    {
        unchecked
        {
            int hash = 23;
            hash = (hash * 31) + Year;
            hash = (hash * 31) + QuarterIndex;
            return hash;
        }
    }
}

Then Distinct works "automagically":

items = items.Distinct().ToList();
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please add this in fiddler?
@user1120418: you mean .NET Fiddle? Here's an ideone demo using the third way: ideone.com/mahzQ0 You can change it to use items = items.Distinct(new ItemComparer()).ToList(); to demonstrate the second approach.
0

This code helps you,

objlist.Where(w => w.ColumnName != "ColumnValue").GroupBy(g => new { g.Value, g.Name }).
                 Select(s=> new ClassName(s.Key.Value, s.Key.Name)).ToList()

Happy Coding :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.