How to ORDER BY non-column field?

Posted by Phil Bolduc on Stack Overflow See other posts from Stack Overflow or by Phil Bolduc
Published on 2012-09-20T06:42:03Z Indexed on 2012/09/20 15:38 UTC
Read the original article Hit count: 184

Filed under:
|

I am trying to create an Entity SQL that is a union of two sub-queries.

(SELECT VALUE DISTINCT ROW(e.ColumnA, e.ColumnB, 1 AS Rank) FROM Context.Entity AS E WHERE ...)
UNION ALL 
(SELECT VALUE DISTINCT ROW(e.ColumnA, e.ColumnB, 2 AS Rank) FROM Context.Entity AS E WHERE ...)
ORDER BY *??*  LIMIT 50

I have tried:

ORDER BY Rank

and

ORDER BY e.Rank

but I keep getting:

System.Data.EntitySqlException: The query syntax is not valid. Near keyword 'ORDER'  

I do not think it is a problem with the Rank column. I do think it is how I am trying to apply an order by to two different esql statements joined by union all. Could someone suggest:

  1. How to apply a ORDER BY to this kind of UNION/UNION ALL statment
  2. How to order by the non-entity column expression.

Thanks.

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about esql