Nhibernate左连接的多个条件

时间:2017-07-21 09:31:11

标签: sql nhibernate

请将此sql查询转换为nhibernate

select     a.ID, count(b.ID)
from       appusers    a
left join  weeklytasks b on a.ID = b.TaskOwner and b.taskstatus = 1
group by   a.ID

1 个答案:

答案 0 :(得分:1)

如果不了解您的实体,映射,使用过的技术(ICreteria API,QueryOver,Linq),很难回答。

但我可以使用QueryOver建议这个解决方案:

AppUser ownerAlias = null;
WeeklyTask taskAlias = null;
var result = Session.QueryOver(() => taskAlias)
                .JoinAlias(x => x.TaskOwner, 
                           () => ownerAlias,
                           NHibernate.SqlCommand.JoinType.RightOuterJoin,
                           Restrictions.Where(() => taskAlias.Status == 1))
                .SelectList(list => list
                    .SelectGroup(x => ownerAlias.Id)
                    .SelectCount(x => x.Id))
                .List<object[]>();

或者这个:

var result = Session.QueryOver<WeeklyTask>()
                .Where(x => x.Status == 1)
                .Right.JoinQueryOver(x => x.TaskOwner)
                .SelectList(list => list
                    .SelectGroup(x => x.TaskOwner.Id)
                    .SelectCount(x => x.Id))
                .List<object[]>();

请注意,在此方法中,您的WeeklyTask实体必须包含对AppUser实体的映射引用。