R# recommends MaxBy that is not supported by Entity Framework

Consider the following code:

var blah = _context.Customers.Where(...).Select(w => new CustomerDTO {
CustomerTrak = new CustomerTrakDTO {
CustomerTrackNote = w.CustomerNavigation.CustomerTrak
.OrderByDescending(t => t.PurchaseDate).First().CustomerNote

R# recommends replacing OrderBy/First with MaxBy. However, Entity Framework Core 6 doesn't support MaxBy! At least, not with SQL Server:

Unhandled exception. System.InvalidOperationException: The LINQ expression 'DbSet<Incarceration>().MaxBy(o => o.DateIn)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly ...

Would be nice if R# was aware of this or could be configured to not suggest breaking change, unless Microsoft supports it in EF

1 comment
Comment actions Permalink

That could get very tricky - the set of supported operations will vary by both the EF Core version and the provider used.

For example, MaxBy will probably be supported by the SQL Server provider in EF Core 7:

Other new LINQ features are also under consideration:

Even supposedly-supported features like GroupBy can be flaky - I've run into several queries that worked in EF6 that had to be rewritten for EF Core 3.x. And often the only way to find that out is to run the query and see if it fails.

Like all ORMs, Entity Framework is a leaky abstraction. Whilst it may be possible that the clever people at JetBrains might be able to do something to improve this, I'm not holding my breath for a perfect solution. 😊


Please sign in to leave a comment.