Search code examples
nhibernatequeryover

What is the difference of SelectList and Projection in Nhibernate?


As the title says.

(To me it looks like SelectList is a way to create projections without using the Projections method.)


Solution

  • I would say that full explanation is in the doc:

    QueryOver allows arbitrary IProjection to be added (allowing private properties to be projected). The Projections factory class also has overloads to allow Lambda Expressions to be used:

    IList selection =
        session.QueryOver<Cat>()
            .Select(Projections.ProjectionList()
                .Add(Projections.Property<Cat>(c => c.Name))
                .Add(Projections.Avg<Cat>(c => c.Age)))
            .List<object[]>();
    

    In addition there is an inline syntax for creating projection lists that does not require the explicit class qualification:

    IList selection =
        session.QueryOver<Cat>()
            .SelectList(list => list
                .Select(c => c.Name)
                .SelectAvg(c => c.Age))
            .List<object[]>();
    

    see Check 16.6. Projections