-
Notifications
You must be signed in to change notification settings - Fork 241
Improve S2971 message to use AsEnumerable in LINQ database query #3604
Description
Description
When calling ToList or ToArray in the middle of a LINQ chain, the rule raised, which is correct in a way but there is an exception that should be managed. When using Entity Framework Core (3.x), it's recommended to manage data on client side as some features can't be parse into SQL.
See: https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.x/breaking-changes#linq-queries-are-no-longer-evaluated-on-the-client
Repro steps
Using EF Core:
return portLinq.OrderBy(v => v.Date).ToList().Select(x => new HistoricalValue(x.Date, x.Value)).ToList();
If we don't retrieve data on client side with the ToList in the middle of the chain, the Select will throw an exception at runtime as it's impossible for EF Core to parse what's in the Select statement.
Expected behavior
Rule shouldn't be raised
Actual behavior
Rule raised