The keyword in this is Queryable.GroupBy
instead of Enumerable.GroupBy
I use EntityFramework and I want to check if there are no duplicate values. Several answers on StackOverflow like this one suggest using GroupBy
IQueryable<MyType> myItems = ...
IQueryable<IGrouping<string, MyType> groupsWithSameName = myItems
.GroupBy(myItem => myItem.Name);
// note: IQueryable!
bool containsDuplicates = groupsWithSameName.Any(group => group.Skip(1).Any());
Although this is allowed on IEnumerables, Skip is not supported on an unordered sequence. The NotSupportedException suggests using OrberBy
before using the Skip.
As an alternative I could check if there are groups with more than one element using Count
bool containsDuplicates = groupsWithSameName.Any(group => group.Count() > 1);
Both methods require to scan all elements in the collection. This is for the 2nd time because they were also scanned to group them.
Is there a method to check for duplicates on an IQueryable more efficiently?