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.
Source: stackoverflow.com
Related Articles
- linq case statement
- How can I achieve SQL CASE statement from LINQ
- LINQ Source Code Available
- LINQ Query with both CASE statement and SUM function
- LINQ Case statement within Count
- SQL to LINQ - Case Statement
- LINQ - CASE statement in WHERE clause
- Writing Case statement in LINQ
- creating Linq to sqlite dbml from DbLinq source code
- Case statement in Linq for a nullable datetime field
- LINQ to SQL Add CASE STATEMENT then original field NOT FOUND
- Case statement in NHibernate / EF Linq
- Convert OrderBy Case statement in LINQ
- Using case statement in linq query
- Linq Expression in Case Statement
- LINQ to SQL With the where with case statement
- source code for LINQ 101 samples
- CASE or IF Statement in Linq Query?
- How to do condition statement in LINQ based on code variables?
- Issue from SQL query case statement to a linq extension methods in C#
- sql to linq where statement with case
- Linq Query using Case statement and concatenate special character
- Query with case statement from SQL Server to Linq query c#
- Translating a sql Case When statement to LINQ
- Using Linq (nHibernate) to perform a Case statement with Counts
- LINQ Case statement with COUNT and GROUP
- Using a dictionary as a source of Regex.Replace patterns in a Linq statement
- How to add a Case statement to my LINQ Query
- Convert SQl to LINQ (with case statement Method)
- c# Linq or code to extract groups from a single list of source data
- Combining two expressions (Expression<Func<T, bool>>)
- Linq is not converting to SQL properly on runtime
- How can I generate this query with Entity Framework to return the corresponding collections?
- Can i use Linq to iterate/filter my web.config AppSettings?
- C# Linq Query - Extracting multiple groups from a collection (nested groups?)
- LINQ:How to flatten deep nested objects with LINQ
- Does Entity Framework perform lambda expressions before or after it fetches the data?
- switch statement in linq
- asp.net core sql where not exist in list
- Entity Framework 6. Query performance continues to degrade
- what is the mechanism for performing an intersect in a Mongo Linq query
- Linq query to nest 3 tables and select certain records
- Lambda or LinQ expression to remove certain objects from List
- Trying to Mock IQueryable Entity Framework Query
- the type arguments for method system linq enumerable as enumerable - Error
- Write a LINQ in C# to filter max x/y in generic PointF list
- How can I do an OrderBy with a dynamic string parameter?
- Using Linq to Determine if List of Uris is Base of another Uri
- Querying a DataTable using LINQ
- LINQ join query destination has List fields