Linq to NHibernate OrderBy Enum value

c#, fluent-nhibernate, linq-to-nhibernate, nhibernate, sql-order-by

Solution

This worked for me. It's more explicit in how NHibernate should handle the status and generate the SQL `case` statement

var workList = session.Query<Item>()
    .OrderBy(item => item.CurrentStatus == Status.Returned ? 1 : 0)
    .ThenBy(item => item.DueDate)
    .ToList();

Problem

I have the following Enum which describes the status of an item and an object that represents the item. ``` public Enum Status { Sent, Received, UnderWork, Returned } public Class Item { public virtual int Id {get;set;} public virtual Status CurrentStatus {get;set;} public virtual string ItemDescription {get;set;} public virtual int ItemNumber {get;set;} public virtual DateTime DueDate {get;set;} } ``` I need to create a query where I can select a number of Items and order them first by the CurrentStatus property and secondly by the DueDate property. The difficulty is I want to order by the CurrentStatus by placing all Items with a Returned Status last while ordered by their DueDate and then order all other Items only by DueDate and ignoring the CurrentStatus. So the following list of data: ``` ID | CurrentStatus | ItemDescription | ItemNumber | DueDate ------------------------------------------------------------- 1 | Returned | Description1 | 123456 | 16/01/2012 2 | Sent | Description2 | 234567 | 13/01/2012 3 | Returned | Description3 | 345678 | 22/01/2012 4 | Received | Description4 | 456789 | 03/01/2012 5 | UnderWork | Description5 | 567891 | 10/01/2012 6 | UnderWork | Description6 | 678901 | 17/01/2012 7 | Sent | Description7 | 789012 | 09/01/2012 8 | Sent | Description8 | 890123 | 28/01/2012 9 | Returned | Description9 | 901234 | 30/01/2012 10 | Received | Description10 | 012345 | 15/01/2012 ``` Would be queried using someting like the following Linq to NHibernate (this is giving me a QuerySyntaxException: Exception of type 'Antlr.Runtime.NoViableAltException' was thrown.) Just to note using this expression in LINQPad I can get the result I require. ``` var workList = session.Query<Item>() .OrderBy(item => item.Status == Status.Returned) .ThenBy(item => item.DueDate) .ToList(); ``` And I expect to get a list of the data in the following order: ``` ID | CurrentStatus | ItemDescription | ItemNumber | DueDate ------------------------------------------------------------- 4 | Received | Description4 | 456789 | 03/01/2012 7 | Sent | Description7 | 789012 | 09/01/2012 5 | UnderWork | Description5 | 567891 | 10/01/2012 2 | Sent | Description2 | 234567 | 13/01/2012 10 | Received | Description10 | 012345 | 15/01/2012 6 | UnderWork | Description6 | 678901 | 17/01/2012 8 | Sent | Description8 | 890123 | 28/01/2012 1 | Returned | Description1 | 123456 | 16/01/2012 3 | Returned | Description3 | 345678 | 22/01/2012 9 | Returned | Description9 | 901234 | 30/01/2012 ``` Can this be done using Linq to NHibernate? And if so what changes do I need to make to my query? Also I will be expecting to SKip() and Take() so I can't just pull everything from the DB. I'm using SQL Server 2008 Express, NHibernate 3.3.0.4000 and Fluent Nhibernate 1.3.0.727. Edit Just to elaborate on how I want the sorting to occur. I want the items with returned status to be pushed to the bottom of the list where they are sorted by the due date. I want all other items to not sort by status and just by due date, they will therefore always come ahead of the returned items. This is because anything returned is no longer in the work process and I want it to be pushed to the end of the list. So in this case the status values other than returned are irrelevant.

Original source