NHibernate的计数与子查询另一个实体? [英] NHibernate count another entity with sub query?

查看:246
本文介绍了NHibernate的计数与子查询另一个实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体分别是:



 用户
{
UserGuid,
地址,



EmailCount //这是未在数据库中的一列,
//我只是想从USEREMAIL表
获得的计数数//并将该值映射到该属性
}

USEREMAIL
{
UserGuid,
时间戳
}

问题是我怎么能获得的电子邮件与NHibernate的子查询计数?



到目前为止我有这个,但它不工作。 ?任何想法

 用户userEntity = NULL; 

无功子查询= QueryOver.Of<&USEREMAIL GT;()
。凡(UE => ue.UserGuid == userEntity.UserGuid)
.ToRowCountQuery();

返回_session.StatefulSession.QueryOver(()=> userEntity)
.WithSubquery.WhereValue(EmailCount)方程(子查询)
的.List()。


解决方案

这是如何使用的方式子查询来得到一个内嵌的计数的有 QueryOver 语法:

 用户userEntity = NULL; 

无功子查询= QueryOver
.Of<&USEREMAIL GT;()
。凡(UE => ue.UserGuid == userEntity.UserGuid)
.ToRowCountQuery( );

无功名单= _session.StatefulSession
.QueryOver<使用者>(()=> userEntity)
.SelectList(预测​​=>预测
。选择( X => x.UserGuid)
.WithAlias(()=> userEntity.UserGuid)
。选择(X => x.Address)
.WithAlias(()=> ; userEntity.Address)
//任何财产被选中
...
// INLINE COUNT
//我们的子查询放入发挥
.SelectSubQuery(子查询)
//这将填充虚拟/不映射EmailCount
.WithAlias(()=> userEntity.EmailCount)

.TransformUsing(Transformers.AliasToBean<使用者>( ))
//.Take(10).Skip(100)//分页
的.List();


I have two entities which are:

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
}

The issue is how can I get the email count with a sub query in NHibernate?

By far I have this, but it does not work. Any idea?

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();

解决方案

This would be the way how to use subquery to get an inlined count with a QueryOver syntax:

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();

这篇关于NHibernate的计数与子查询另一个实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆