Fluent NHibernate Select Max of Count Grouped by property

Viewed 211

I'd like to get the max of the count of some grouped by query with Projections or Subqueries. Is it possible?

     AEntity aAlias = null;
     BEntity bAlias = null;
     var cntQuery = await _session.QueryOver<AEntity>()
        .Left.JoinAlias(pv => pv.BEntity, () => bAlias)
        .Select(
            Projections.Group<AEntity>(e => e.Id),
            Projections.Count(Projections.Property<AEntity>(x => x.Id)))
        .ListAsync(ct);

So this returns something like this:

+----------------------------------------+-------+
|                   id                   | Count |
+----------------------------------------+-------+
| "af517a65-18c2-4e9f-9df6-a537cc5c9c92" |     5 |
| "48bf681d-2ccd-4df8-b0e5-b2c3f418e3d0" |     1 |
| "c0699258-9f2b-4ce6-a895-91d759cbde29" |     3 |
| "0959f6b4-b365-43fa-aede-25df327a27d1" |     2 |
+----------------------------------------+-------+

I need the max of these but wouldn't like to use:

  • order desc and take
  • LINQ after ListAsync
  • SQL query
1 Answers

Can you try this and see if it works?

var results = session.QueryOver<AEntity>()
        .Left..JoinQueryOver(pv => pv.BEntity, () => bAlias)
        .SelectList(list => list
            .SelectGroup(pv => pv.Id)
            .SelectCount(() => pv.Id)
        )
        .List<object[]>();
Related