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

3 comments
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:
https://github.com/dotnet/efcore/issues/25566

Other new LINQ features are also under consideration:
https://github.com/dotnet/efcore/issues/25570

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. 😊

1
Comment actions Permalink

Hello Felix,

As far as Richard has provided reasonable comment I'd just suggest turning the inspection off for Entity Framework projects.

I've also submitted a feature request - https://youtrack.jetbrains.com/issue/RSRP-489111 so we can investigate if we can improve this behavior. You are welcome to comment and vote for it.

Thank you.

0
Comment actions Permalink

Thank you Angelina! With all due respect, *all* projects are Entity Framework projects! So, your recommendation doesn't make sense. Hopefully, "clever people at JetBrains might be able to do something to improve this". If not - just give user some way to narrowly override the recommendation

0

Please sign in to leave a comment.