How to Order an NHibernate 3.0 Linq Request Based on the Name of the Delivered Property

I am trying to order a dynamic NHibernate 3.0 Linq query based on the name of a column 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 keyword orderbytakes a string (or variable), but when I do the following:

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

I get this exception:

request failed

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 element identified by ORDER BY number 1 contains the variable as part of the expression identifying the position of the column. Variables are only allowed when ordering an expression that refers to a column name.

LINQ Dynamic Query Library, .OrderBy, , , . NHibernate 3.0.

, ORDER BY SQL- , NHibernate.

NHibernate, , NHibernate Linq, .

+3
2

LINQ , .

, .

+4

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.

+1

Source: https://habr.com/ru/post/1786715/


All Articles