如何根据提供的属性名称订购NHibernate 3.0 Linq查询(How to order a NHibernate 3.0 Linq query based on a supplied prop

编程入门 行业动态 更新时间:2024-10-22 07:45:50
如何根据提供的属性名称订购NHibernate 3.0 Linq查询(How to order a NHibernate 3.0 Linq query based on a supplied property name)

我正在尝试根据存储在字符串变量中的列名动态地命令我的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.

最满意答案

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

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

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

That's what we are using right now.

更多推荐

本文发布于:2023-08-07 19:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465585.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   名称   NHibernate   Linq   property

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!