我正在尝试根据存储在字符串变量中的列名动态排序我的 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;
我得到这个异常:
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.
我知道有 LINQ Dynamic Query Library它提供了 .OrderBy
扩展方法的重载,该扩展方法接受字符串 ,但显然只能在内存中使用。但是,当使用 NHibernate 3.0 时它会抛出异常。
我试图让生成的 SQL 查询中的 ORDER BY
语句指定适当的列名,因此我需要留在 NHibernate 领域。
使用 NHibernate 标准,我可以动态排序,但由于我使用的是 NHibernate Linq,我似乎无法访问标准功能。
最佳答案
Dynamic LINQ 适用于任何源,而不仅仅是内存。
这就是我们现在正在使用的。
关于.net - 如何根据提供的属性名称订购 NHibernate 3.0 Linq 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4749689/