NHibernate QueryOver select entity and aggregates

前端 未结 1 547
感动是毒
感动是毒 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 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()
         .JoinAlias(o => o.LineItems, () => items)
         .Select(Projections.ProjectionList()
          .Add(Projections.Property(o=>o.Id).WithAlias(()=>orderDto.OrderId))
          .Add(Projections.Property(o=>o.DateOrdered).WithAlias(()=>orderDto.DateOrdered))
          .Add(Projections.Count(()=> items.Id).WithAlias(()=>orderDto.ItemCount))
         )
         .TransformUsing(Transformers.AliasToBean())
        .List();
    

    0 讨论(0)
提交回复
热议问题