c# - Linq to NHibernate OrderBy 枚举值

标签 c# nhibernate fluent-nhibernate sql-order-by linq-to-nhibernate

我有以下枚举,它描述了一个项目的状态和一个代表该项目的对象。

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;}
}

我需要创建一个查询,我可以在其中选择多个项目并首先按 CurrentStatus 属性对它们进行排序,然后按 DueDate 属性对它们进行排序。困难在于我想按 CurrentStatus 进行排序,方法是将所有具有返回状态的项目放在最后,同时按其 DueDate 订购,然后仅按 DueDate 订购所有其他项目并忽略 CurrentStatus。

所以数据列表如下:

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

将使用类似以下 Linq to NHibernate 的方式进行查询(这给了我一个 QuerySyntaxException:抛出类型为 'Antlr.Runtime.NoViableAltException' 的异常。)请注意在 LINQPad 中使用此表达式我可以获得我的结果需要。

var workList = session.Query<Item>()
                .OrderBy(item => item.Status == Status.Returned)
                .ThenBy(item  => item.DueDate)
                .ToList();

我希望按以下顺序获得数据列表:

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

这可以使用 Linq to NHibernate 来完成吗?如果是这样,我需要对查询进行哪些更改?我也期待 SKip() 和 Take() 所以我不能只从数据库中提取所有内容。我正在使用 SQL Server 2008 Express、NHibernate 3.3.0.4000 和 Fluent Nhibernate 1.3.0.727。

编辑

只是详细说明我希望如何进行排序。我希望将具有退回状态的项目推到列表的底部,在那里它们按截止日期排序。我希望所有其他项目不按状态排序,只按截止日期排序,因此它们将始终排在退回项目之前。这是因为任何返回的东西都不再在工作过程中,我希望它被推到列表的末尾。所以在这种情况下,返回的状态值是无关紧要的。

最佳答案

这对我有用。 NHibernate 应该如何处理状态并生成 SQL case 语句

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

关于c# - Linq to NHibernate OrderBy 枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10819653/

相关文章:

c# - 用 Roslyn 为某个方法的调用者提供信息

c# - Entity Framework 计数子实体

nhibernate - 为查询定义子选择

c# - 无法将 HBM​​ 映射到多对多的 FluentNHibernate 映射

nhibernate - Fluent nhibernate 映射嵌套属性 "cannot find getter"

c# - Fluently Hibernate DB2SQL配置

c# - 如何在MVC 4项目中制作显示模板

c# - 泛型和类型转换

nhibernate - Linq 查询中的内连接获取

c# - NHibernate - DateTime.DayOfYear LINQ