简体   繁体   English

Linq到NHibernate OrderBy枚举值

[英]Linq to NHibernate OrderBy Enum value

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. 我需要创建一个查询,我可以在其中选择多个Items并首先通过CurrentStatus属性对其进行排序,然后再按DueDate属性进行排序。 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. 困难在于我想通过CurrentStatus订购,最后按DueDate排序所有具有返回状态的项目,然后仅通过DueDate订购所有其他项目并忽略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. 将使用以下Linq到NHibernate进行查询(这给了我一个QuerySyntaxException:抛出了类型'Antlr.Runtime.NoViableAltException'的异常。)只是要注意在LINQPad中使用这个表达式我可以得到我需要的结果。

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? 可以使用Linq到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. 此外,我将期待SKip()和Take(),所以我不能从数据库中提取所有内容。 I'm using SQL Server 2008 Express, NHibernate 3.3.0.4000 and Fluent Nhibernate 1.3.0.727. 我正在使用SQL Server 2008 Express,NHibernate 3.3.0.4000和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. 因此,在这种情况下,返回的状态值不相关。

This worked for me. 这对我有用。 It's more explicit in how NHibernate should handle the status and generate the SQL case statement NHibernate应该如何处理状态并生成SQL case语句更明确

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

There seems to be the problem with your LINQ query. 你的LINQ查询似乎有问题。 Not sure if you used that by intention, but shouldn't you have .OrderBy(item => item.Status) instead of .OrderBy(item => item.Status == Status.Returned) ? 不确定你是否按意图使用了,但不应该有.OrderBy(item => item.Status)而不是.OrderBy(item => item.Status == Status.Returned) That's probably confusing NHibernate's expression parser. 这可能会让NHibernate的表达式解析器感到困惑。

How did you map your enum to DB? 你是如何将你的枚举映射到数据库的? To an int column (default)? int列(默认)? Or as a string? 还是作为一个字符串?

And could you explain the expected list? 你能解释一下预期的清单吗? Shouldn't you be having Sent items first, then Received , then UnderWork and finally Returned , considering that you do order by status? 您是否应该首先Sent项目,然后Received ,然后是UnderWork并最终Returned ,考虑到您按状态订购? Your expected list doesn't look sorted correctly. 您的预期列表看起来不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM