Share via


How to remove duplicate value from list using linq

Question

Wednesday, November 27, 2019 8:01 PM

Please share a code samole where linq Will be used to remove duplicate value from list

All replies (8)

Thursday, November 28, 2019 7:06 AM ✅Answered

Hi Sudip_inn, 

Thank you for posting here.

As Kareninstructor and DA924x suggested, ‘Distinct’ , ‘IEqualityComparer’ and so on are choices for different situations to remove duplicate value in list.

I will make a summary and provide some simple examples.

If the type of data in your list is string , int or so on,  you can use ‘Distinct’ method directly.

Example:

            List<string> lst = new List<string>() {"A","B","C","A" };
            var result = lst.Distinct().ToList();

Result:

If the type of data in your list is class, you need to set the standard of ‘duplicate’ between two classes.

I make two tests and you can refer to them.

  1. Implement the IEquatable<T> generic interface in the class.

Example:

    class Program
    {
        static void Main(string[] args)
        {
            List<Student> stuLst = new List<Student>() { 
                new Student(){ Id =1,Name = "A",Age = 10},
               new Student(){ Id =2,Name = "B",Age = 12},
                new Student(){ Id =3,Name = "C",Age = 13},
                new Student(){ Id =1,Name = "A",Age = 11}
            };
            var results = stuLst.Distinct().ToList();
            foreach (var result in results)
                Console.WriteLine(result.Id + " " + result.Name + " " + result.Age);
            Console.ReadLine();
        }
    }

    class Student : IEquatable<Student>
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }

        /// <summary>
        /// Standard: If one class's Id and Name is the same as antoher class, then they are duplicate.
        /// </summary>
        public bool Equals(Student other)
        {
            if (Object.ReferenceEquals(other, null)) return false;

            if (Object.ReferenceEquals(this, other)) return true;

            return Id.Equals(other.Id) && Name.Equals(other.Name);
        }
        // If Equals() returns true for a pair of objects  
        // then GetHashCode() must return the same value for these objects. 
        public override int GetHashCode()
        {
            return Id.GetHashCode() ^ Name.GetHashCode();
       }
    }

Result:

  1. Create a class which implement IEqualityComparer<T> interface to compare values.

Example:

    class Program
    {
        static void Main(string[] args)
        {
            List<Student> stuLst = new List<Student>() {
                new Student(){ Id =1,Name = "A",Age = 10},
                new Student(){ Id =2,Name = "B",Age = 12},
                new Student(){ Id =3,Name = "C",Age = 13},
                new Student(){ Id =1,Name = "A",Age = 11}
            };

            var results = stuLst.Distinct(new StudentComparer()).ToList();

            foreach (var result in results)
                Console.WriteLine(result.Id + " " + result.Name + " " + result.Age);
            Console.ReadLine();
        }
    }
    class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
    class StudentComparer : IEqualityComparer<Student>
    {
        /// <summary>
        /// Standard: If one class's Id and Name is the same as antoher class, then they are duplicate.
        /// </summary>
        public bool Equals(Student x, Student y)
        {
            if (Object.ReferenceEquals(x, y)) return true;
            if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                return false;
            return x.Id == y.Id && x.Name == y.Name;
        }
        public int GetHashCode(Student student)
        {
            if (Object.ReferenceEquals(student, null)) return 0;
            return student.Id.GetHashCode() ^ student.Name.GetHashCode();
        }
    }

Result:

Hope them can help you.

Best Regards,

Xingyu Zhao

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Wednesday, November 27, 2019 9:12 PM | 1 vote

Hello,

The response would be in tune with what type of list e.g. list of string, list of a concrete class e.g. Person class and when dealing with concrete classes what defines a duplicate.

For string a simple way to get non-duplicates is using Distinct while with a class you may need to implement IEqualityComparer interface.

For sample code see my GitHub repository

https://github.com/karenpayneoregon/WorkingWithCollections

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow


Wednesday, November 27, 2019 9:36 PM | 1 vote

Please share a code samole where linq Will be used to remove duplicate value from list

https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.distinct?view=netframework-4.8

http://dotnetpattern.com/linq-distinct-operator

http://vmsdurano.com/various-ways-to-get-distinct-values-from-a-listt-using-linq/


Thursday, November 28, 2019 7:01 AM

This depends little bit from requirements since getting distinct items from list is different thing than removing duplicates from list. So do you want to remove duplicates from existing list so that list remains same or just get distinct items in list?


Thursday, November 28, 2019 11:50 AM

instead of distinct() how could i remove duplicate from List<T>


Friday, November 29, 2019 12:49 AM

instead of distinct() how could i remove duplicate from List<T>

var listints= new List<int>();

listint =  otherints.Distinct().ToList();


Sunday, December 1, 2019 6:41 PM

List<Student> stuLst = new List<Student>() { 
                new Student(){ Id =1,Name = "A",Age = 10},
               new Student(){ Id =2,Name = "B",Age = 12},
                new Student(){ Id =3,Name = "C",Age = 13},
                new Student(){ Id =1,Name = "A",Age = 11}
            };
            var results = stuLst.Distinct().ToList();

can't we delete rest from the list stuLst after getting list of distinct data ? i mean can i delete rest from stuLst except data found in results?


Monday, December 2, 2019 1:35 AM

Hi Sudip_inn,

you can try the following code.

            List<Student> stuLst = new List<Student>() {
                new Student(){ Id =1,Name = "A",Age = 10},
               new Student(){ Id =2,Name = "B",Age = 12},
                new Student(){ Id =3,Name = "C",Age = 13},
                new Student(){ Id =1,Name = "A",Age = 11}
            };

            // select Id and Name from the list.
            var results = stuLst.Distinct().Select(x => new { x.Id, x.Name }).ToList();

            foreach (var result in results)
                Console.WriteLine(result.Id + " " + result.Name);

Result:

Hope it could be helpful.

Best Regards,

Xingyu Zhao

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].