376

I have a class Items with properties (Id, Name, Code, Price).

The List of Items is populated with duplicated items.

For ex.:

1         Item1       IT00001        $100
2         Item2       IT00002        $200
3         Item3       IT00003        $150
1         Item1       IT00001        $100
3         Item3       IT00003        $150

How to remove the duplicates in the list using linq?

3
  • I have another class as property in the Items Class also
    – Prasad
    Oct 22, 2009 at 11:54
  • You can also do var set = new HashSet<int>(); var uniques = items.Where(x => set.Add(x.Id));. It should be criminal to do so..
    – nawfal
    Oct 3, 2013 at 10:00
  • I agree.. ForEach should be used instead :) Feb 4 at 17:24

11 Answers 11

731
var distinctItems = items.GroupBy(x => x.Id).Select(y => y.First());
6
  • 40
    Thanks - was looking to avoid writing a comparer class so I'm glad this works :)
    – Jen
    Oct 5, 2011 at 4:29
  • 12
    +1 This solution even allows for a tie-breaker: eliminate duplicates with criteria! Sep 5, 2013 at 1:05
  • 6
    But a little overhead! Mar 15, 2014 at 20:00
  • 2
    But, as Victor Juri suggested below: use FirstorDefault. can not believe, that solution can be so simple (without custom equality comparer)
    – CyberHawk
    Mar 16, 2015 at 13:55
  • 9
    You can group with multiple properties : List<XYZ> MyUniqueList = MyList.GroupBy(x => new { x.Column1, x.Column2 }).Select(g=> g.First()).ToList(); Oct 10, 2017 at 7:26
454
var distinctItems = items.Distinct();

To match on only some of the properties, create a custom equality comparer, e.g.:

class DistinctItemComparer : IEqualityComparer<Item> {

    public bool Equals(Item x, Item y) {
        return x.Id == y.Id &&
            x.Name == y.Name &&
            x.Code == y.Code &&
            x.Price == y.Price;
    }

    public int GetHashCode(Item obj) {
        return obj.Id.GetHashCode() ^
            obj.Name.GetHashCode() ^
            obj.Code.GetHashCode() ^
            obj.Price.GetHashCode();
    }
}

Then use it like this:

var distinctItems = items.Distinct(new DistinctItemComparer());
9
  • Hi Christian , What will be the change in code if i have a List<my_Custom_Class> and List<string>. My custom class has various items in which one is DCN number and list<string> has only DCN number. So I need to check the List<Custom_Class> contains any dcn from List<string>. For example suppose List1 = List<Custom_Class> and List2 = List<String>. If List1 has 2000 items and list2 has 40000 items on which 600 items from List1 exists in List2. So in this case i need 1400 as my output List as list1. So what would be the expression. Thanks in advance
    – user240141
    Aug 11, 2010 at 2:54
  • Also one more case is here since List1 contains various items , other items values might be different but the DCN must be same. So in my case Distinct failed to give desired out put.
    – user240141
    Aug 11, 2010 at 2:57
  • 3
    I find comparer classes extremely useful. They can express logic other than simple property name comparisons. I wrote a new one last month, to do something that GroupBy could not. Aug 19, 2013 at 7:22
  • Works well and got me to learn something new and investigate the XoR operator ^ in C#. Had used in VB.NET via Xor but had to do a double take to your code to see what it was at first.
    – atconway
    May 2, 2014 at 15:51
  • This is the error I get when I try to use Distinct Comparer: "LINQ to Entities does not recognize the method 'System.Linq.IQueryable1[DataAccess.HR.Dao.CCS_LOCATION_TBL] Distinct[CCS_LOCATION_TBL](System.Linq.IQueryable1[DataAccess.HR.Dao.CCS_LOCATION_TBL], System.Collections.Generic.IEqualityComparer`1[DataAccess.HR.Dao.CCS_LOCATION_TBL])' method, and this method cannot be translated into a store expression. Nov 13, 2015 at 16:38
47

If there is something that is throwing off your Distinct query, you might want to look at MoreLinq and use the DistinctBy operator and select distinct objects by id.

var distinct = items.DistinctBy( i => i.Id );
2
  • 1
    There is no DistinctBy() method with Linq.
    – fbarikzehy
    May 2, 2016 at 6:03
  • 14
    @FereydoonBarikzehy But he is not talking about pure Linq. In post is linq to MoreLinq project ...
    – Ademar
    Aug 10, 2016 at 8:30
34

This is how I was able to group by with Linq. Hope it helps.

var query = collection.GroupBy(x => x.title).Select(y => y.FirstOrDefault());
2
  • 3
    @nawfal, I was suggesting FirstOrDefault() in lieu of First()
    – sobelito
    Jun 30, 2014 at 19:21
  • 35
    If I am correct, using FirstOrDefault here offers no benefit if the Select immediately follows GroupBy, since there's no possibility of there being an empty group (the groups were just derived from the collection's contents)
    – Roy Tinker
    Nov 18, 2015 at 23:28
27

An universal extension method:

public static class EnumerableExtensions
{
    public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> enumerable, Func<T, TKey> keySelector)
    {
        return enumerable.GroupBy(keySelector).Select(grp => grp.First());
    }
}

Example of usage:

var lstDst = lst.DistinctBy(item => item.Key);
3
  • 1
    Very clean approach Aug 23, 2019 at 15:30
  • 2
    Thanks this is exactly what I was looking for, works well.
    – mig_08
    Nov 30, 2020 at 8:42
  • perfect in addition with @Salah Akbari solution : result = result.DistinctBy(r => new { r.NDA, r.Event, r.MovementDate, r.EventDate }).ToList();
    – Flou
    Jul 22, 2022 at 11:20
20

You have three option here for removing duplicate item in your List:

  1. Use a a custom equality comparer and then use Distinct(new DistinctItemComparer()) as @Christian Hayter mentioned.
  2. Use GroupBy, but please note in GroupBy you should Group by all of the columns because if you just group by Id it doesn't remove duplicate items always. For example consider the following example:

    List<Item> a = new List<Item>
    {
        new Item {Id = 1, Name = "Item1", Code = "IT00001", Price = 100},
        new Item {Id = 2, Name = "Item2", Code = "IT00002", Price = 200},
        new Item {Id = 3, Name = "Item3", Code = "IT00003", Price = 150},
        new Item {Id = 1, Name = "Item1", Code = "IT00001", Price = 100},
        new Item {Id = 3, Name = "Item3", Code = "IT00003", Price = 150},
        new Item {Id = 3, Name = "Item3", Code = "IT00004", Price = 250}
    };
    var distinctItems = a.GroupBy(x => x.Id).Select(y => y.First());
    

    The result for this grouping will be:

    {Id = 1, Name = "Item1", Code = "IT00001", Price = 100}
    {Id = 2, Name = "Item2", Code = "IT00002", Price = 200}
    {Id = 3, Name = "Item3", Code = "IT00003", Price = 150}
    

    Which is incorrect because it considers {Id = 3, Name = "Item3", Code = "IT00004", Price = 250} as duplicate. So the correct query would be:

    var distinctItems = a.GroupBy(c => new { c.Id , c.Name , c.Code , c.Price})
                         .Select(c => c.First()).ToList();
    

    3.Override Equal and GetHashCode in item class:

    public class Item
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Code { get; set; }
        public int Price { get; set; }
    
        public override bool Equals(object obj)
        {
            if (!(obj is Item))
                return false;
            Item p = (Item)obj;
            return (p.Id == Id && p.Name == Name && p.Code == Code && p.Price == Price);
        }
        public override int GetHashCode()
        {
            return String.Format("{0}|{1}|{2}|{3}", Id, Name, Code, Price).GetHashCode();
        }
    }
    

    Then you can use it like this:

    var distinctItems = a.Distinct();
    
0
17

Use Distinct() but keep in mind that it uses the default equality comparer to compare values, so if you want anything beyond that you need to implement your own comparer.

Please see http://msdn.microsoft.com/en-us/library/bb348436.aspx for an example.

1
  • I should notice that default comparer works if collection member types is one of value types. But which default equality comparer select by csc for reference types. Reference types must have own comparer(s). Mar 3, 2017 at 19:20
6

Try this extension method out. Hopefully this could help.

public static class DistinctHelper
{
    public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
    {
        var identifiedKeys = new HashSet<TKey>();
        return source.Where(element => identifiedKeys.Add(keySelector(element)));
    }
}

Usage:

var outputList = sourceList.DistinctBy(x => x.TargetProperty);
0
3
List<Employee> employees = new List<Employee>()
{
    new Employee{Id =1,Name="AAAAA"}
    , new Employee{Id =2,Name="BBBBB"}
    , new Employee{Id =3,Name="AAAAA"}
    , new Employee{Id =4,Name="CCCCC"}
    , new Employee{Id =5,Name="AAAAA"}
};

List<Employee> duplicateEmployees = employees.Except(employees.GroupBy(i => i.Name)
                                             .Select(ss => ss.FirstOrDefault()))
                                            .ToList();
0

Another workaround, not beautiful buy workable.

I have an XML file with an element called "MEMDES" with two attribute as "GRADE" and "SPD" to record the RAM module information. There are lot of dupelicate items in SPD.

So here is the code I use to remove the dupelicated items:

        IEnumerable<XElement> MList =
            from RAMList in PREF.Descendants("MEMDES")
            where (string)RAMList.Attribute("GRADE") == "DDR4"
            select RAMList;

        List<string> sellist = new List<string>();

        foreach (var MEMList in MList)
        {
            sellist.Add((string)MEMList.Attribute("SPD").Value);
        }

        foreach (string slist in sellist.Distinct())
        {
            comboBox1.Items.Add(slist);
        }
-1

When you don't want to write IEqualityComparer you can try something like following.

 class Program
{

    private static void Main(string[] args)
    {

        var items = new List<Item>();
        items.Add(new Item {Id = 1, Name = "Item1"});
        items.Add(new Item {Id = 2, Name = "Item2"});
        items.Add(new Item {Id = 3, Name = "Item3"});

        //Duplicate item
        items.Add(new Item {Id = 4, Name = "Item4"});
        //Duplicate item
        items.Add(new Item {Id = 2, Name = "Item2"});

        items.Add(new Item {Id = 3, Name = "Item3"});

        var res = items.Select(i => new {i.Id, i.Name})
            .Distinct().Select(x => new Item {Id = x.Id, Name = x.Name}).ToList();

        // now res contains distinct records
    }



}


public class Item
{
    public int Id { get; set; }

    public string Name { get; set; }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.