0

Consider the following entities:

Package        Manifest       Content
----------     ----------     ----------     
Id             PackageId      Id
Name           ContentId      Name
                              Status

The relationship is many-to-many where a package can have multiple contents and contents can belong to multiple packages.

What I'd like to do is, for a given Package.Id, get the count of all Content which have a specific Status.

My mapping works fine, but I have not been able to figure out how to achieve a conditional count of the Content without loading the Content. One option is to use an ApplyChildFilter in the mapping and use with ExtraLazyLoad, but this means I'd have to create a dedicated property just for this.

Is there a way to achieve this without falling back on CreateSQLQuery?

Charles Chen
  • 1,395
  • 13
  • 22

1 Answers1

0

Does the following query work for you?

  session
    .QueryOver<Package>()
    .JoinQueryOver(x => x.Manifest)
    .JoinQueryOver(x => x.Content)
    .Where(x => x.Status == someStatus)
    .Select(Projections.RowCount())
    .FutureValue<int>()
    .Value
Robert
  • 1,466
  • 10
  • 25
  • the issue is that the Manifest is not explicitly mapped since it's implicitly mapped via `HasManyToMany.Table("Manifest").ParentKeyColumn("PackageId").ChildKeyColumn("ContentId")`. I think your proposal would work if I added an explicit mapping and property for the Manifest, but I was hoping for some way to avoid that (right now just using SQL to basically do this JOIN). – Charles Chen Mar 01 '16 at 20:26