NHibernate用子查询计算另一个实体?

swa*_*ive 3 c# sql nhibernate queryover

我有两个实体:

User
{
    UserGuid,
    Address,
    .
    .
    .
    EmailCount // This is not a column in the database, 
               // I just wanna get the count number from the UserEmail table
               // and map the value to this property
}

UserEmail
{
    UserGuid,
    Timestamp
}
Run Code Online (Sandbox Code Playgroud)

问题是如何通过NHibernate中的子查询获取电子邮件数量?

到目前为止我有这个,但它不起作用.任何的想法?

User userEntity = null;

var subQuery = QueryOver.Of<UserEmail>()
    .Where(ue => ue.UserGuid == userEntity.UserGuid)
    .ToRowCountQuery();                       

return _session.StatefulSession.QueryOver(() => userEntity)
          .WithSubquery.WhereValue("EmailCount").Eq(subQuery)
          .List();
Run Code Online (Sandbox Code Playgroud)

Rad*_*ler 5

这将是如何使用语法subquery获取内联计数的方法QueryOver:

User userEntity = null;

var subQuery = QueryOver
    .Of<UserEmail>()
    .Where(ue => ue.UserGuid == userEntity.UserGuid)
    .ToRowCountQuery(); 

var list = _session.StatefulSession
    .QueryOver<User>(() => userEntity)
    .SelectList(projections => projections
        .Select(x => x.UserGuid)
             .WithAlias(() => userEntity.UserGuid)
        .Select(x => x.Address)
             .WithAlias(() => userEntity.Address)
        // any property to be selected
        ... 
        // INLINE COUNT
        // our subquery placed into play
        .SelectSubQuery(subQuery)
             // this will populate virtual/not mapped EmailCount
             .WithAlias(() => userEntity.EmailCount) 
    )
    .TransformUsing(Transformers.AliasToBean<User>())
    //.Take(10).Skip(100) // paging
    .List();
Run Code Online (Sandbox Code Playgroud)