Fluent Nhibernate - selecting specific column and count query with group by

安稳与你 提交于 2019-12-10 21:08:09

问题


I'm having some trouble excuting a query in fluent nhibernate. I have a table : Books with the following columns:

ID, NAME, YEAR, BOOK_TYPE, AUTHOR_ID

I want to excute the following sql query in Fluent NHibernate:

SELECT BOOK_TYPE, COUNT(*)
FROM BOOKS
GROUP BY BOOK_TYPE

回答1:


So called Fluent-NHibernate is just a mapping extension. To get data we need NHibernate built n querying features: ICriteria, QueryOver or even a LINQ.

Based on the documentation we can use projections for the above case, using the QueryOver API

16.6. QueryOver - Projections

The code snippet:

IList selection =
    session.QueryOver<Book>()
        .SelectList(list => list
            .Select(c => c.BooktType)
            .SelectCount(c => c.ID))
        .List<object[]>();


来源:https://stackoverflow.com/questions/33982582/fluent-nhibernate-selecting-specific-column-and-count-query-with-group-by

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!