Using LINQ, from a List, how can I retrieve a list that contains entries repeated more than once and their values?
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 {
public T Key { get; set; }
public int Count { get; set; }
}
public static IQueryable Duplicates(this IQueryable source, Expression> groupBy)
=> source.GroupBy(groupBy).Where(w => w.Count() > 1).Select(s => s.Key);
public static IQueryable GetDuplicates(this IQueryable source, Expression> groupBy)
=> source.GroupBy(groupBy).Where(w => w.Count() > 1).SelectMany(s => s);
public static IQueryable> DuplicatesCounts(this IQueryable source, Expression> groupBy)
=> source.GroupBy(groupBy).Where(w => w.Count() > 1).Select(y => new CountOfT { Key = y.Key, Count = y.Count() });
public static IQueryable> DuplicatesCountsAsTuble(this IQueryable source, Expression> groupBy)
=> source.GroupBy(groupBy).Where(w => w.Count() > 1).Select(s => Tuple.Create(s.Key, s.Count()));
}