It is easy to choose the first of each group:
var firstOfEachGroup = dbContext.Measurements .OrderByDescending(m => m.MeasurementId) .GroupBy(m => new { m.SomeColumn }) .Where(g => g.Count() > 1) .Select(g => g.First());
But...
Question : how can I select everything from each group except the first element?
var everythingButFirstOfEachGroup = dbContext.Measurements .OrderByDescending(m => m.MeasurementId) .GroupBy(m => new { m.SomeColumn }) .Where(g => g.Count() > 1) .Select( ...? );
Additional Information:
My real goal is to remove all duplicates except the last one (in bulk, RemoveRange .: do not use the built-in foreach), so after the previous request I want to use RemoveRange :
dbContext.Measurements.RemoveRange(everythingButFirstOfEachGroup);
So, if my question does not make sense, this information may be convenient.
source share