title: How to make EF core translate custom sort into a plain 'When Then' instead of hierarchical
body: <p>The <strong>problem</strong> is - I get the following <strong>errror</strong>:</p>
  
  <blockquote>
    <p>Case expressions may only be nested to level 10.</p>
  </blockquote>
  
  <p>when I try using the following LINQ code with EF core</p>
  
  <pre><code>var extendedResult = result.Select(p =&gt; new
      {
          readModelFields = p,
          sortKey =
              p.CaseState == CaseState.Scheduled.ToString() ? 1 :
              p.CaseState == CaseState.Queued.ToString() ? 2 :
              p.CaseState == "Investigation" ? 3 :
              p.CaseState == "Awaiting Customer" ? 4 :
              p.CaseState == "State 5" ? 5 :
              p.CaseState == "State 6" ? 6 :
              p.CaseState == "State 7" ? 7 :
              p.CaseState == "State 8" ? 8 :
              p.CaseState == "State 9" ? 9 :
              p.CaseState == "State 10" ? 10 :
              p.CaseState == "State 11" ? 11 :
              p.CaseState == "Rejected" ? 12 :
              p.CaseState == "Blocked" ? 13 :
              p.CaseState == "Postponed" ? 14 :
              15
      });
  
      return IsAscending(query.SortDirection)
          ? extendedResult.OrderBy(p =&gt; p.sortKey).Select(p =&gt; p.readModelFields)
          : extendedResult.OrderByDescending(p =&gt; p.sortKey).Select(p =&gt; p.readModelFields);
  </code></pre>
  
  <p>And the issue is caused by the way SQL genertes SQL (here's a result)</p>
  
  <pre><code>exec sp_executesql N'SELECT [o].[Id], [o].[Owner], [o].[CaseState],  [o].[LastActionDate], [o].[CountryCode], [o].[Name], [o].[Risk]
  FROM [dbo].[OpenCases] AS [o]
  ORDER BY CASE
      WHEN [o].[CaseState] = N''Scheduled'' THEN 1
      ELSE CASE
          WHEN [o].[CaseState] = N''Queued'' THEN 2
          ELSE CASE
              WHEN [o].[CaseState] = N''Investigation'' THEN 3
              ELSE CASE
                  WHEN [o].[CaseState] = N''Awaiting Customer'' THEN 4
                  ELSE CASE
                      WHEN [o].[CaseState] = N''State 6'' THEN 6
                      ELSE CASE
                          WHEN [o].[CaseState] = N''State 7'' THEN 7
                          ELSE CASE
                              WHEN [o].[CaseState] = N''State 8'' THEN 8
                              ELSE CASE
                                  WHEN [o].[CaseState] = N''State 9'' THEN 9
                                  ELSE CASE
                                      WHEN [o].[CaseState] = N''State 10'' THEN 10
                                      ELSE CASE
                                          WHEN [o].[CaseState] = N''State 11'' THEN 11
                                          ELSE CASE
                                              WHEN [o].[CaseState] = N''Rejected'' THEN 12
                                              ELSE CASE
                                                  WHEN [o].[CaseState] = N''Blocked'' THEN 13
                                                  ELSE CASE
                                                      WHEN [o].[CaseState] = N''Postponed'' THEN 14
                                                      ELSE 15
                                                  END
                                              END
                                          END
                                      END
                                  END
                              END
                          END
                      END
                  END
              END
          END
      END
  END DESC
  OFFSET @__p_0 ROWS FETCH NEXT @__p_1 ROWS ONLY',N'@__p_0 int,@__p_1 int',@__p_0=0,@__p_1=2147483647
  </code></pre>
  
  <p>What I want instead - is EF to build a query like this:</p>
  
  <pre><code>...
  ORDER BY 
  CASE
     WHEN CaseState = "Investigation" THEN 3
     WHEN CaseState = "State 5" THEN 5
     WHEN CaseState = "State 6" THEN 6
     WHEN CaseState = "State 7" THEN 7
     .......
     WHEN CaseState = "Postponed" THEN 14
  END desc
  </code></pre>