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

5 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
Comment actions Permalink

The suggestion to use MaxBy is problematic more generally in that Resharper makes this suggestion and replaces code where the result is not actually the same.

take for example a line like:

var lastItem = data.Items.OrderByDescending(s => s.DisplayOrder).FirstOrDefault();

R# suggests changing this line to:

var lastItem = data.Items.MaxBy(s => s.DisplayOrder);

But this is incorrect. The line suggested by R# will throw if the list happens to be empty!

Introducing subtle bugs like this is really bad form for a tool that's supposed to be improving code.

 

 

 

 

0
Comment actions Permalink

Hello Tyco Kaine,

 

Please refer to the following issue already reported and our developer's comment on it - https://youtrack.jetbrains.com/issue/RSRP-489528.

Thank you.

0

Please sign in to leave a comment.