How to combine two flat lists into one nested object

后端 未结 4 523
-上瘾入骨i
-上瘾入骨i 2021-01-25 12:25

I have three lists which contains three same properties in each collection. I want to combine a result into one collection. Ex classes structure is as below

publ         


        
4条回答
  •  情深已故
    2021-01-25 12:59

    What are you describing sounds like two standard multi-key LINQ group joins. They are quite efficient (LINQ to Objects implementation uses prepared fast hash based lookups), so no further optimizations are needed:

    var orderDetails = (
        from o in data.Orders
        join p in data.PaymentDetails
            on new { o.ProductId, o.CustomerId, o.OrderId }
            equals new { p.ProductId, p.CustomerId, p.OrderId }
            into orderPaymentDetails
        join c in data.CouponUsageDetails
            on new { o.ProductId, o.CustomerId, o.OrderId }
            equals new { c.ProductId, c.CustomerId, c.OrderId }
            into orderCouponUsageDetails
        select new OrderDetails
        {
            ProductId = o.ProductId,
            CustomerId = o.CustomerId,
            OrderId = o.OrderId,
            // Few other Properties of OrderDetail
            PaymentDetail = orderPaymentDetails.ToList(),
            CouponUsageDetail = orderCouponUsageDetails.ToList(),
        })
        .ToList();
    

提交回复
热议问题