如何在Nhibernate QueryOver中获取带有子计数的父实体列表 [英] How to get List of Parent Entities with Child Count In Nhibernate QueryOver

查看:66
本文介绍了如何在Nhibernate QueryOver中获取带有子计数的父实体列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两节课:

    public class Parent
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<Child> Childrens { get; set; }
    }

    public class Child
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

现在通过Nhibernate QueryOver,我想在单个查询中获取所有不包含子代数的所有父代的列表.

Now through Nhibernate QueryOver I want to get list of all Parent with no of Count of children in single query.

预期输出是?:

ParentId  Name ChildrenCount
1         ABC      10
2         CDE      5

任何人都可以帮助我.

推荐答案

使用此DTO进行投影:

Using this DTO for projection:

public class ParentDto
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int ChildrenCount { get; set; }
}

使用此查询:

Child childAlias = null;
ParentDto dto = null;

var dtoParents = Session.QueryOver<Parent>()
    .JoinAlias(x => x.Childrens, () => childAlias)
    .SelectList(list => list
        .SelectGroup(x => x.Id).WithAlias(() => dto.Id)
        .SelectGroup(x => x.Name).WithAlias(() => dto.Name)
        .SelectCount(() => childAlias.Id).WithAlias(() => dto.ChildrenCount))
    .TransformUsing(Transformers.AliasToBean<ParentDto>())
    .List<ParentDto>();

您可以使用DTO QueryOver投影的信息"nofollow noreferrer>此处.

You can read more about QueryOver projections using DTOs here.

这篇关于如何在Nhibernate QueryOver中获取带有子计数的父实体列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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