如何根据提供的属性名称订购 NHibernate 3.0 Linq 查询

发布于 2024-10-13 05:01:22 字数 1407 浏览 3 评论 0 原文

我正在尝试根据存储在字符串变量中的列名动态排序 NHibernate 3.0 Linq 查询。

// The value of this variable can be the name of any property of Document.
string columnName = "column1";

var query = from n in Session.Query<Document>()
            where n.DocumentNumber == documentNumber
            // how to order by the value of columnName?
            select n;

orderby 关键字确实接受字符串(或变量),但是当我执行以下命令时:

var query = from n in Session.Query<Document>()
            where n.DocumentNumber == documentNumber
            orderby columnName
            select n;

我收到此异常:

无法执行查询

select TOP (@p0)
  accumulate0_.Id as Id9_, 
  accumulate0_.DocumentNumber as Documen10_9_
from dbo.Documents accumulate0_
where
  accumulate0_.DocumentNumber=@p1
order by @p2 desc

由 ORDER BY 编号 1 标识的 SELECT 项包含一个变量作为一部分 标识列位置的表达式的。仅当以下情况时才允许变量 通过引用列名的表达式进行排序。

我知道有 LINQ 动态查询库,它提供了 .OrderBy 扩展方法的重载,该方法接受字符串 但显然只在内存中工作 。然而,当使用 NHibernate 3.0 时它会抛出异常。

我试图让生成的 SQL 查询中的 ORDER BY 语句指定适当的列名,因此我需要留在 NHibernate 领域。

使用 NHibernate 标准,我可以动态排序,但由于我使用 NHibernate Linq,我似乎无法访问标准功能。

I'm trying to order my NHibernate 3.0 Linq query dynamically, based on a column name stored in a string variable.

// The value of this variable can be the name of any property of Document.
string columnName = "column1";

var query = from n in Session.Query<Document>()
            where n.DocumentNumber == documentNumber
            // how to order by the value of columnName?
            select n;

The orderby keyword does accept a string (or variable) but when I execute the following:

var query = from n in Session.Query<Document>()
            where n.DocumentNumber == documentNumber
            orderby columnName
            select n;

I get this exception:

could not execute query

select TOP (@p0)
  accumulate0_.Id as Id9_, 
  accumulate0_.DocumentNumber as Documen10_9_
from dbo.Documents accumulate0_
where
  accumulate0_.DocumentNumber=@p1
order by @p2 desc

The SELECT item identified by the ORDER BY number 1 contains a variable as part
of the expression identifying a column position. Variables are only allowed when
ordering by an expression referencing a column name.

I know there's the LINQ Dynamic Query Library which provides an overload of the .OrderBy extention method that accepts a string but that obviously only works in memory. It throws an exception when using NHibernate 3.0 however.

I'm trying to have the ORDER BY statement in the generated SQL query have the appropriate column name specified, so I need to stay in the NHibernate realm.

Using NHibernate criteria, I can order dynamically but since I'm using NHibernate Linq, I don't seem to have access to criteria features.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪漫人生路 2024-10-20 05:01:22

动态 LINQ 适用于任何源,而不仅仅是内存中。

这就是我们现在正在使用的。

Dynamic LINQ works with any source, not just in memory.

That's what we are using right now.

意犹 2024-10-20 05:01:22

但是由于我使用 NHibernate Linq,所以我
似乎无法访问标准
特点。

当然是会话。应该提出 CreateCriteria? NHibernate Linq 和 CreateCriteria 在一起应该会更加幸福。

不确定是否可以单独使用 linq,我也不认为可以使用 QueryOver,但一种方法可能是使用 CreateCriteria,正如您所指出的。

var query = Session
    .CreateCriteria<Document>()
    .Add(Restrictions.Eq("DocumentNumber", documentNumber))
    .AddOrder(Order.Asc("column1"))
    .List<Document>();

另一种方法是使用 HQL。

but since I'm using NHibernate Linq, I
don't seem to have access to criteria
features.

Surely Session. should bring up CreateCriteria? NHibernate Linq and CreateCriteria should live quite happier together.

Not sure if is it possible using linq alone, also I don't think it is possible using QueryOver but one way may be to use CreateCriteria as you have pointed out.

var query = Session
    .CreateCriteria<Document>()
    .Add(Restrictions.Eq("DocumentNumber", documentNumber))
    .AddOrder(Order.Asc("column1"))
    .List<Document>();

Another way would be to use HQL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文