C# LINQ find duplicates in List

前端 未结 9 1113
星月不相逢
星月不相逢 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:23

    Find out if an enumerable contains any duplicate :

    var anyDuplicate = enumerable.GroupBy(x => x.Key).Any(g => g.Count() > 1);
    

    Find out if all values in an enumerable are unique :

    var allUnique = enumerable.GroupBy(x => x.Key).All(g => g.Count() == 1);
    
    0 讨论(0)
  • 2020-11-22 08:24

    The easiest way to solve the problem is to group the elements based on their value, and then pick a representative of the group if there are more than one element in the group. In LINQ, this translates to:

    var query = lst.GroupBy(x => x)
                  .Where(g => g.Count() > 1)
                  .Select(y => y.Key)
                  .ToList();
    

    If you want to know how many times the elements are repeated, you can use:

    var query = lst.GroupBy(x => x)
                  .Where(g => g.Count() > 1)
                  .Select(y => new { Element = y.Key, Counter = y.Count() })
                  .ToList();
    

    This will return a List of an anonymous type, and each element will have the properties Element and Counter, to retrieve the information you need.

    And lastly, if it's a dictionary you are looking for, you can use

    var query = lst.GroupBy(x => x)
                  .Where(g => g.Count() > 1)
                  .ToDictionary(x => x.Key, y => y.Count());
    

    This will return a dictionary, with your element as key, and the number of times it's repeated as value.

    0 讨论(0)
  • 2020-11-22 08:27

    there is an answer but i did not understand why is not working;

    var anyDuplicate = enumerable.GroupBy(x => x.Key).Any(g => g.Count() > 1);
    

    my solution is like that in this situation;

    var duplicates = model.list
                        .GroupBy(s => s.SAME_ID)
                        .Where(g => g.Count() > 1).Count() > 0;
    if(duplicates) {
        doSomething();
    }
    
    0 讨论(0)
  • 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<T> getMoreThanOnceRepeated<T>(this IEnumerable<T> extList, Func<T, object> 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<T> getAllRepeated<T>(this IEnumerable<T> extList, Func<T, object> 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<Person> PersonsLst = new List<Person>(){
        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
            */
    }
    
    0 讨论(0)
  • 2020-11-22 08:34

    Complete set of Linq to SQL extensions of Duplicates functions checked in MS SQL Server. Without using .ToList() or IEnumerable. These queries executing in SQL Server rather than in memory.. The results only return at memory.

    public static class Linq2SqlExtensions {
    
        public class CountOfT<T> {
            public T Key { get; set; }
            public int Count { get; set; }
        }
    
        public static IQueryable<TKey> Duplicates<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> groupBy)
            => source.GroupBy(groupBy).Where(w => w.Count() > 1).Select(s => s.Key);
    
        public static IQueryable<TSource> GetDuplicates<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> groupBy)
            => source.GroupBy(groupBy).Where(w => w.Count() > 1).SelectMany(s => s);
    
        public static IQueryable<CountOfT<TKey>> DuplicatesCounts<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> groupBy)
            => source.GroupBy(groupBy).Where(w => w.Count() > 1).Select(y => new CountOfT<TKey> { Key = y.Key, Count = y.Count() });
    
        public static IQueryable<Tuple<TKey, int>> DuplicatesCountsAsTuble<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> groupBy)
            => source.GroupBy(groupBy).Where(w => w.Count() > 1).Select(s => Tuple.Create(s.Key, s.Count()));
    }
    
    0 讨论(0)
  • 2020-11-22 08:39

    Linq query:

    var query = from s2 in (from s in someList group s by new { s.Column1, s.Column2 } into sg select sg) where s2.Count() > 1 select s2;
    
    0 讨论(0)
提交回复
热议问题