LINQ: Finding out duplicates in the list with multiple fields

I needed very quick simple “one line” solution to filter out duplicates in my list. So I have a List<T> where T is complex object with multiple properties. I wanted to filter out non unique items based on two fields. In database analogy my list is table with two columns based composite primary key and I need to select non-distinct values.

Here is sample code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqDuplicates
    public class Item
        public int ID { get; set; }
        public string Name { get; set; }

    class Program
        static void Main(string[] args)
            List items = new List();
            items.Add(new Item { ID = 0, Name = "John" });
            items.Add(new Item { ID = 1, Name = "Thorsten" });
            items.Add(new Item { ID = 1, Name = "Thorsten" });
            items.Add(new Item { ID = 2, Name = "Rapunzel" });
            items.Add(new Item { ID = 3, Name = "Rapunzel" });
            items.Add(new Item { ID = 3, Name = "Grinch" });

            var duplicates = items.GroupBy(x => new {x.ID, x.Name})
                            .Where(g => g.Count() > 1)
                            .Select(g => g.Key);

            foreach (var val in duplicates)
                Console.WriteLine(val.ID + " " + val.Name);


So this code will select just one duplicate I have in list and that is Item with ID=1 and Name=”Thorsten”.

Another way around to get all distinct values would be:

var distinct = items.Select(item => new { item.ID, item.Name }).Distinct();

This results in list with all the distinct values based on two fields (composite key) criteria.


blog comments powered by Disqus

About me

Bizic Bojan is Freelance Software Development Consultant with focus on C++, Qt and .NET based software solutions development. 



The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.