LINQ equivalent of my SQL Query for UNION ALL

RookieAppler
(SELECT * FROM SheetHay WHERE SheetStatus = 2)
 UNION ALL(SELECT * FROM SheetHay WHERE SheetStatus = 1)
 UNION ALL (SELECT * FROM SheetHay WHERE SheetStatus  = 0)
 UNION ALL(SELECT * FROM SheetHay WHERE SheetStatus= 3)

I get result set like this: enter image description here

I mean I get all '2' together, '0' together, '3' together ( no '1' in table yet). But when I use LINQ and UNION them I see the result on my grid in order of SheetID, the primary key. I mean I see the order of sheets displayed as 15,23,25,27,28,29 etc. But I want it just as the SQL result set.23,43,25,28 etc

IEnumerable<SheetHay> listTwos = Get(LINQ QUERY TO GET just twos);    
IEnumerable<SheetHay> listOnes = Get(LINQ QUERY TO GET just Ones);
IEnumerable<SheetHay> listZeros = Get(LINQ QUERY TO GET just  Zeros);
IEnumerable<SheetHay> listThrees = Get(LINQ QUERY TO GET just Threes);
....
    return listTwos.Union(listZeros).Union(listOnes).Union(listThrees);

Let me know if you need any other information. Thanks.

Tim Schmelter

You don't need to use multiple queries you can use CASE in the ORDER BY in sql and a similar way in LINQ.

SQL:

SELECT * FROM SheetHay 
WHERE SheetStatus IN(0,1,2,3)) 
ORDER BY CASE SheetStatus 
    WHEN 2 THEN 1 
    WHEN 1 THEN 2 
    WHEN 0 THEN 3 
    WHEN 3 THEN 4 END ASC, SheetStatus ASC

LINQ:

int[] status =  {0, 1, 2, 3};
var query = db.SheetHay 
    .Where(s => status.Contains(s.SheetStatus))
    .OrderByDescending(s => s.SheetStatus == 2)
    .ThenByDescending(s =>  s.SheetStatus == 1)
    .ThenByDescending(s =>  s.SheetStatus == 0)
    .ThenByDescending(s =>  s.SheetStatus == 3)
    .ThenBy(s =>  s.SheetStatus);

Descending because a comparison returns bool and true is "higher" than false(1/0).

You could also use a conditional operator to return an int for the ordering:

var query = db.SheetHay 
    .Where(s => status.Contains(s.SheetStatus))
    .OrderBy(s => s.SheetStatus == 2 ? 0 : 1)
    .ThenBy(s =>  s.SheetStatus == 1 ? 0 : 1)
    .ThenBy(s =>  s.SheetStatus == 0 ? 0 : 1)
    .ThenBy(s =>  s.SheetStatus == 3 ? 0 : 1)
    .ThenBy(s =>  s.SheetStatus);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Linq union all equivalent of sql code

From Dev

What is SQL equivalent to LINQ .All()

From Dev

Sql Query: Union All with Top

From Dev

What is phpMyAdmin doing to my UNION ALL query?

From Dev

Whats is Linq equivalent for SQL query with OR-joins?

From Dev

Collation conflict in SQL Union All Query

From Dev

Collation conflict in SQL Union All Query

From Dev

SQL subquery combine with UNION ALL query

From Dev

SQL Server query: Union vs Distinct union all performance

From Dev

LINQ Union vs SQL Union

From Dev

VB. NET: LINQ to SQL equivalent to Count - group by query

From Dev

What is the equivalent code of SQL query in C# with linq to datatable?

From Dev

Why does this Linq Query Return Different Results than SQL Equivalent?

From Dev

getting my SQL query to be displayed in LINQ form

From Dev

Why does my linq to sql query fail?

From Dev

Linq with Lambda equivalent of SQL

From Dev

SQL limitation equivalent in linq

From Dev

LINQ equivalent of SQL NOT IN statement

From Dev

SQL limitation equivalent in linq

From Dev

MS Access SQL: Using SELECT INTO with a UNION ALL query

From Dev

remove the last "UNION ALL" in building up a sql query with foreach loop

From Dev

Multiple recursive union all selects in a CTE SQL query

From Dev

T-SQL [UNION ALL] removing records from query result

From Dev

Grouping [Union All] result from multiple columns SQL query

From Dev

Multiple recursive union all selects in a CTE SQL query

From Dev

access-SQL-Query - Using "Order By" in UNION ALL

From Dev

Why is my SQL Union Query Returning Different Values than my manual SELECT WHERE query?

From Dev

SQL query with UNION refinement

From Dev

SQL query - union on NOW()

Related Related

HotTag

Archive