NHibernate QueryOver select entity and aggregates

前端 未结 1 546
感动是毒
感动是毒 2021-01-03 00:00

What I want to do is display a simple grid of data which contains the entity data, and the aggregate data of its children. For example lets use a Order and line items. I wan

相关标签:
1条回答
  • 2021-01-03 00:40

    Given:

    public class Order
    {
        public virtual int OrderId {get; set;}
        public virtual DateTime OrderDate {get; set;}
        public virtual IList<LineItem> LineItems {get; set;}
    }
    public class LineItem
    {
        public virtual int Id {get; set;}
        public virtual string Description {get; set;}
    }
    

    To query a projection of Order + Aggregated LineItem using QueryOver API you can do the following:

    OrderDto orderDto = null;
    LineItem items = null;
    var results = session.QueryOver<Order>()
         .JoinAlias(o => o.LineItems, () => items)
         .Select(Projections.ProjectionList()
          .Add(Projections.Property<Order>(o=>o.Id).WithAlias(()=>orderDto.OrderId))
          .Add(Projections.Property<Order>(o=>o.DateOrdered).WithAlias(()=>orderDto.DateOrdered))
          .Add(Projections.Count(()=> items.Id).WithAlias(()=>orderDto.ItemCount))
         )
         .TransformUsing(Transformers.AliasToBean<OrderDto>())
        .List<OrderDto>();
    
    0 讨论(0)
提交回复
热议问题