NHibernate QueryOver: Get a row count with group by in a subquery

后端 未结 1 1069
自闭症患者
自闭症患者 2021-02-19 19:47

I\'m trying to get a count from a query with a group by and just can\'t figure out how to translate the SQL I want into NHibernate\'s QueryOver syntax.

This is the SQL:<

1条回答
  •  难免孤独
    2021-02-19 20:02

    I'm not sure why you need such a complex query. If you only want the count of distinct emails meeting certain criteria I think you could use something like this in SQL:

    select count(distinct email)
    from Entry
    where (conditions...)
    

    And translating this to NHibernate's QueryOver API would look something like this:

    int count = session.QueryOver()
                .Select(Projections.CountDistinct(x => x.Email))
                .FutureValue()
                .Value;//query is not executed until here
    

    Unless I'm missing something, I think this will get the result you're after. There is also a "Distinct" projection and a .ToRowCountQuery() method that you might find interesting.

    0 讨论(0)
提交回复
热议问题