简体   繁体   English

NHibernate Linq扩展命令子对象集合

[英]NHibernate linq extensions order child objects collection

I'm trying to query my db with NH Linq extensions and here is where I am stuck: 我正在尝试使用NH Linq扩展名查询我的数据库,这是我遇到的问题:

I have a Customer object and it has a property of type Order , and Order has an ICollection<Orderline> collection, and lastly Orderline object has a property of type int called ' price '. 我有一个Customer对象,它的属性类型为OrderOrder有一个ICollection<Orderline>集合,最后, Orderline对象的类型为int的属性称为“ price ”。 What I want to do is, given a customerId, I should get all customer information(Order and Orderline) and the collection of Orderline objects should be ordered by Orderline's price property. 我想要做的是,给定一个customerId,我应该获取所有客户信息(Order和Orderline),并且Orderline对象的集合应该由Orderline's price属性排序。 I've left out all other details for clarity. 为了清楚起见,我省略了所有其他细节。

Note: In this case the relation between Customer and Order is 1 to 1 . 注意:在这种情况下, CustomerOrder之间的关系是1 to 1 A customer can have only 1 order, an Order may contain many Orderlines as you may infer from the collection type. 一位顾客只能有1个订单,一个Order可能包含许多Orderlines ,你可以从集合类型推断。

Many thanks for any help... 非常感谢您的帮助...

var query = from customer in session.Query<Customer>()
            let order = customer.Order
            from orderline in order.Orderlines
            orderby orderlines.price
            select new
            {
                CustomerId = customer.Id,
                CustomerName = customer.Name,
                OrderId = order.Id,
                OrderLineId = orderline.Id,
                Price = orderline.Price,
            };

var results = query.ToLookup(a => a.CustomerId)
    .Select(g => new CustomerDto
    {
        Id = g.Key,
        CustomerName = g.First().CustomerName,
        OrderLines =  g.Select(a => new OrderLineDto(a.OrderLineId, a.Price)).ToList()
    }).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM