C# LINQ find duplicates in List

前端 未结 9 1205
星月不相逢
星月不相逢 2020-11-22 07:57

Using LINQ, from a List, how can I retrieve a list that contains entries repeated more than once and their values?

9条回答
  •  独厮守ぢ
    2020-11-22 08:29

    I created a extention to response to this you could includ it in your projects, I think this return the most case when you search for duplicates in List or Linq.

    Example:

    //Dummy class to compare in list
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public Person(int id, string name, string surname)
        {
            this.Id = id;
            this.Name = name;
            this.Surname = surname;
        }
    }
    
    
    //The extention static class
    public static class Extention
    {
        public static IEnumerable getMoreThanOnceRepeated(this IEnumerable extList, Func groupProps) where T : class
        { //Return only the second and next reptition
            return extList
                .GroupBy(groupProps)
                .SelectMany(z => z.Skip(1)); //Skip the first occur and return all the others that repeats
        }
        public static IEnumerable getAllRepeated(this IEnumerable extList, Func groupProps) where T : class
        {
            //Get All the lines that has repeating
            return extList
                .GroupBy(groupProps)
                .Where(z => z.Count() > 1) //Filter only the distinct one
                .SelectMany(z => z);//All in where has to be retuned
        }
    }
    
    //how to use it:
    void DuplicateExample()
    {
        //Populate List
        List PersonsLst = new List(){
        new Person(1,"Ricardo","Figueiredo"), //fist Duplicate to the example
        new Person(2,"Ana","Figueiredo"),
        new Person(3,"Ricardo","Figueiredo"),//second Duplicate to the example
        new Person(4,"Margarida","Figueiredo"),
        new Person(5,"Ricardo","Figueiredo")//third Duplicate to the example
        };
    
        Console.WriteLine("All:");
        PersonsLst.ForEach(z => Console.WriteLine("{0} -> {1} {2}", z.Id, z.Name, z.Surname));
        /* OUTPUT:
            All:
            1 -> Ricardo Figueiredo
            2 -> Ana Figueiredo
            3 -> Ricardo Figueiredo
            4 -> Margarida Figueiredo
            5 -> Ricardo Figueiredo
            */
    
        Console.WriteLine("All lines with repeated data");
        PersonsLst.getAllRepeated(z => new { z.Name, z.Surname })
            .ToList()
            .ForEach(z => Console.WriteLine("{0} -> {1} {2}", z.Id, z.Name, z.Surname));
        /* OUTPUT:
            All lines with repeated data
            1 -> Ricardo Figueiredo
            3 -> Ricardo Figueiredo
            5 -> Ricardo Figueiredo
            */
        Console.WriteLine("Only Repeated more than once");
        PersonsLst.getMoreThanOnceRepeated(z => new { z.Name, z.Surname })
            .ToList()
            .ForEach(z => Console.WriteLine("{0} -> {1} {2}", z.Id, z.Name, z.Surname));
        /* OUTPUT:
            Only Repeated more than once
            3 -> Ricardo Figueiredo
            5 -> Ricardo Figueiredo
            */
    }
    

提交回复
热议问题