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 Query
- How can I achieve SQL CASE statement from LINQ
- SQL to LINQ - Case Statement
- LINQ to SQL Add CASE STATEMENT then original field NOT FOUND
- LINQ to SQL With the where with case statement
- Issue from SQL query case statement to a linq extension methods in C#
- sql to linq where statement with case
- Query with case statement from SQL Server to Linq query c#
- Translating a sql Case When statement to LINQ
- Convert SQl to LINQ (with case statement Method)
- C# Convert SQL query with Case statement to LINQ
- Translate a SQL CASE Statement into LINQ
- SQL to Linq Where Case Statement
- complex case statement using LinQ to sql
- How are people unit testing code that uses Linq to SQL
- linq case statement
- What is the easiest way to find the LINQ statement for a SQL statement
- if exists statement in sql to linq
- Linq version of SQL "IN" statement
- LINQ is Generating Extra IS NULL Condition in SQL Statement
- How do you do a SQL style 'IN' statement in LINQ to Entities (Entity Framework) if Contains isn't supported?
- Equivalent of SQL Between Statement Using Linq or a Lambda expression
- Translate LINQ to sql statement
- Linq To Sql - Dynamic OrderBy - Case When
- LINQ Source Code Available
- Is LINQ generally slower than a equal SQL statement
- Case Query in LINQ equivalent to sql query
- Converting sql statement that contains 'with' cte to linq
- Linq to SQL Case WHEN in VB.NET?
- LINQ Query with both CASE statement and SUM function
- SQL IN statement in Linq for NHibernate
More Query from same tag
- Entity Framework - An item with the same key has already been added. - Error when trying to define foreign key relationship
- Linq GroupBy objects with an if else condition
- Get selected values of a CheckBoxList
- Creating an anonymous object via LINQ from IObservable?
- LINQ Group By Multiple fields -Syntax help
- Using Linq to get value from a model asp.net mvc
- enum bitwise/flags Instance enumeration/iteration/foreach
- Including Select Count(*) in Linq Query VB.NET
- How to convert nested loop to a LINQ-statement?
- How to group by on 2 child entities and get total of both this child entities?
- How can I loop Through all the elements in .select statement in Linq
- Grouping data with dynamic keys with Linq
- Linq query to return filtered data
- Inconsitency error inside Entity Framework , "Collection was modified; enumeration operation may not execute."
- how to use substring in linq c# and also bind convert html tag in plan text. issue in binding GridView in asp.net
- Get records associated to null value if no match is found using lambda expression
- c# wpf linq items quantity in carts shown in datagrid
- DbFunctions.DiffDays() results in: This function can only be invoked from LINQ to Entities
- Linq Display Sort lidt
- How to create dynamic query with for loop?
- How to delete a single row from database using linq query?
- What is the most efficient way to get comparison statistics for data from 2 large tables
- using Single/First methods to return multiple values in linq or lambda expression
- converting SQL join with group by and MAX to Linq
- C# How to read variable from list
- Remove Single Quotes From All Cells in a DataTable - Creating New Table - Using LINQ in Vb.Net
- Efficient way to get previous n day by date data from LINQ/EF
- C# - Get most common string in a Model list
- Async and LINQ Queries: Retrieve all lines in all files in a directory tree
- Order By Date print Latest 20 records only in LINQ