nhibernatequeryovernhibernate-projections

NHibernate: ProjectionList: Can we create dynamic projectionlist for Orderby


I am working on a query in NHibernate in which user could provide a sorting order for some selected fields. I need to do a OrderBy() in QueryOver with the names of field names in entities, but when using projection list I am getting like this.

SELECT
     this_.Number as y0_,
     scc3_.Code as y1_,

FROM sometable
WHERE 1=1 ORDER BY this_.Number as y0_,
 scc3_.Code as y1_ asc

The projection list for select columns is different from orderby projection list and I am not using .WithAlias()

sortProjectionList.Add(Projections.Property(() => stock.Number));
sortProjectionList.Add(Projections.Property(() => scc.Code));

How can I create a projectionlist without aliases or aliases with custom names?

Thanks


Solution

  • I expect, that you experience an issue with generated keyword "AS" inside of the ORDER BY in case, that you pass projections like this:

    // NHibernate will leave few AS, except the last
    query.OrderBy(sortProjectionList).Asc();
    

    To avoid that, we can do it like this:

    // NHibernate will solve each projection separately
    for (var index = 0; index < sortProjectionList.Length; index++)
    {
        query.OrderBy(sortProjectionList[index]).Asc();
    }