How to get List of Parent Entities with Child Count In Nhibernate QueryOver

删除回忆录丶 提交于 2019-12-05 14:23:49

Using this DTO for projection:

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

Use this query:

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

You can read more about QueryOver projections using DTOs here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!