If you explicitly want to use the record with the lowest id in each set of duplicates, you can use
var duplicates = companies .GroupBy(c => new { c.Name, c.Email }) .Select(g => new { Qty = g.Count(), First = g.OrderBy(c => c.Id).First() } ) .Select(p => new { Id = p.First.Id, Qty = p.Qty, Name = p.First.Name, Email = p.First.Email, Address = p.First.Address });
If you do not care what record values ββare used, or if your source is already sorted by ID (ascending), you can refuse to call OrderBy .
Rawling
source share