score:2
You are experiencing EF Core query translation bug, tracked by #12729: Flatten CASE expressions to avoid error "Case expressions may only be nested to level 10". As we can see, it's already fixed in the EF Core repository, but the fix will be included in 5.0.0 release (whenever it comes).
It's possible to fix it by hooking into EF Core internal infrastructure, but that requires knowing the exact EF Core version and also use different approaches for different EF Core versions because the infrastructure is changing with almost every even minor release.
As a general workaround I could suggest replacing the single multi-conditional operator expression with sum of multiple single conditional expressions. It won't generate the desired flat CASE
SQL expression, but at least won't produce the error in question. Something like this:
sortKey =
(p.CaseState == CaseState.Scheduled.ToString() ? 1 : 0) +
(p.CaseState == CaseState.Queued.ToString() ? 2 : 0) +
(p.CaseState == "Investigation" ? 3 : 0) +
(p.CaseState == "Awaiting Customer" ? 4 : 0) +
(p.CaseState == "State 5" ? 5 : 0) +
(p.CaseState == "State 6" ? 6 : 0) +
(p.CaseState == "State 7" ? 7 : 0) +
(p.CaseState == "State 8" ? 8 : 0) +
(p.CaseState == "State 9" ? 9 : 0) +
(p.CaseState == "State 10" ? 10 : 0) +
(p.CaseState == "State 11" ? 11 : 0) +
(p.CaseState == "Rejected" ? 12 : 0) +
(p.CaseState == "Blocked" ? 13 : 0) +
(p.CaseState == "Postponed" ? 14 : 15)
score:0
How about using a string map to convert the states?
var stateMapString = "Investigation Awaiting CustomerState 5 State 6 State 7 State 8 State 9 State 10 State 11 Rejected Blocked Postponed";
var extendedResult = result.Select(p => new
{
readModelFields = p,
sortKey = p.CaseState == CaseState.Scheduled.ToString() ? 1 :
p.CaseState == CaseState.Queued.ToString() ? 2 :
(p.CaseState != "" && stateMapString.IndexOf(p.CaseState) >= 0)
? stateMapString.IndexOf(p.CaseState)/17+3
: 15
});
If your states might change, you could compute the map string:
var stateStrings = new[] { "Investigation",
"Awaiting Customer",
"State 5",
"State 6",
"State 7",
"State 8",
"State 9",
"State 10",
"State 11",
"Rejected",
"Blocked",
"Postponed" };
var stateMaxLen = stateStrings.Max(s => s.Length);
var stateMapString = String.Join("", stateStrings.Select(s => s.PadRight(stateMaxLen)));
And just use stateMaxLen
in the query to divide the IndexOf
result.
Source: stackoverflow.com
Related Query
- How to make EF core translate custom sort into a plain 'When Then' instead of hierarchical
- How do I tell linq2db how to translate a given expression, ie Split(char) into SQL when it does not know how to do so?
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How to make this SQL query into DotNet Core 2.1 Linq
- How to select top result for a given ID then join that into another table EF Core LINQ
- How much of a performane hit will i take from casting when trying to make this code mistake proof?
- How to sort by a specific key and then get the first result in the sorted collection into a group in LINQ
- how to translate a linq expression into sql string use c# code
- How do I sort strings alphabetically while accounting for value when a string is numeric?
- How do I translate this GROUP BY / MIN SQL query into LINQ?
- How do I translate a query that uses ROW_NUMBER() into linq?
- How to parse XML data into the properties of a custom C# class?
- How can I make sure my LINQ queries execute when called in my DAL, not in a delayed fashion?
- How to reuse a linq expression for 'Where' when using multiple source tables
- How to make multiple includes more efficient with EF Core 3.0
- How can I combine this code into one or two LINQ queries?
- How to query a list of data to make rows into columns
- How to Translate SQL "WHERE expr IN (query)" into LINQ?
- Make Entity Framework (using Linq queries) use alias of custom field instead of redoing the subquery
- How to use Linq to sort an array by Length and then value
- How to make a SUM of Dictionary Value nested into a list with LINQ?
- How to get SQL query into LINQ form in C# code
- How can I concatenate all the column values in a row, and then concatenate all rows in a DataTable into a single string?
- How do I get this Linq query with custom join into a DataSet?
- How to group then select elements into new class in LINQ (C# preferably)
- how to sort a list then sort a subset of that list
- How to avoid repeating property projections when using EF Core inheritance?
- How to access argument properties when writing custom NHibernate HQL generator
- How do i translate this SQL into LINQ? (Sorting by popularity)
- how to combine two data source into one?
More Query from same tag
- Tranform and Apply Expression via LINQ?
- Casting IOrderedEnumerable to ICollection
- Linq Query where related entity contains value from array
- Eager Loading with Entity Framework, LINQ
- Group items by month and by date
- Combine two Linq lambda expressions
- How do i use navigation properties correctly EF?
- Should LINQ be avoided because it's slow?
- About AsEnumerable() behavior
- set default value for nulls in result of a linq join
- Eager Loading Lambda Error in EF 6.0.1
- .net Core 3.1 Linq Expression "...could not be translated." Exception
- Can anyone help me in completing this generic method to update from one list to another list?
- How retrieve deeper siblings using LINQ to XML?
- Accessing nested child nodes?
- How does LINQ contains work
- Linq very slow query (never completed)
- Linq to Sql, System.Exception cast is not valid
- Issue getting different types value of self join table
- Transform to Linq (overlap validations)
- How to populate a Class with Dictionary using LINQ 3
- EBay OData Type Provider in F# and getting no results with LINQ
- Load list with linq to xml
- LINQ to SQL error message: 'Where' not found
- Linq -Where clause with not exists?
- LINQ GroupBy two object properties and SUM, AVG, or Ignore others using Lambda
- Generic repository. Need suggestion
- Linq select in query in c#
- How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python?
- can't shorten the interface