score:1

Accepted answer

Try using ternary operator for all cases in your ThenBy clause:

 .ThenBy(x => x.column3 == "X" 
      ? 1 
      : x.column3 == "Y" 
          ? 2 
          : x.column3 == "Z" 
                ? 3 
                : defaultOrder)

score:0

It'll be kind of yucky, but...

context.TableModel.Where(x => x.ID == 10)
    .OrderBy(x => x.column1)
    .ThenBy(x=> x.column3 == "X" ? 1
        : x.column3 == "Y" ? 2
        : x.column3 == "Z" ? 3
        : defaultOrder)
    .AsNoTracking()
    .ToListAsync();

Basically just a giant nested if statement.
Also, the select statement is redundant if your table only has three columns.

My suggestion is to make an enum out of all your column3 values and then orderby that instead. It'll be more efficient because enum assigns int values to each item.


Related Articles