I have a database entity Foo that has the following properties:
public partial class Foo
{
public string Name { get; set; }
public string Type { get; set; }
etc...
}
I want to get the records where Type is one of a set of Types, and where there are two or more records for the same Name that meet this criteria.
The LINQ statement I have is:
var records = databaseContext.Foo
.Where(r => listOfTypes.Contains(r.Type))
.GroupBy(r => r.Name)
.Where(x => x.Count() > 1)
.SelectMany(g => g);
However, this gives an exception due to EF Core no longer falling back to client-side evaluation:
System.InvalidOperationException: Processing of the LINQ expression 'g => g' by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core. See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information.
I can force client-side evaluation by adding an .AsEnumerable() at the start of the LINQ statement, but this is horrendously slow and inefficient, taking over an hour to process the 20K+ records in my Foo table
How can I rewrite this so that it can be translated to SQL, or get this result some other way?