如何使用LINQ从父母列表中选择和组复杂的子对象 [英] How to use Linq to select and group complex child object from a parents list

查看:187
本文介绍了如何使用LINQ从父母列表中选择和组复杂的子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用LINQ的选择,并从父母清单组复杂的子对象。

How to use Linq to select and group complex child object from a parents list.

我有一个 OrderList 每个订单的对象有一个 OrderProductVariantList (OrderLineList),以及每个 OrderProductVariant 对象有ProductVariant,然后在 ProductVariant 对象有一个的产品对象,它包含产品信息。

I have an OrderList each of order object has a OrderProductVariantList(OrderLineList), and each of OrderProductVariant object has ProductVariant, and then the ProductVariant object will have a Product object which contains product information.

我的目标是选择和组最流行的产品从订单列表。

谁能帮我?

非常感谢。

推荐答案

您的描述是难以遵循,但我认为你只是想出来的产品和他们发生次数排名。 。会的SelectMany对您有帮助。

Your description is hard to follow, but I think you just want to get out the Products and rank them by the number of times they occur. SelectMany will be helpful for this.

 var query = orderList.SelectMany( o => o.OrderLineList )
                        // results in IEnumerable<OrderProductVariant>
                      .Select( opv => opv.ProductVariant )
                      .Select( pv => p.Product )
                      .GroupBy( p => p )
                      .Select( g => new {
                                    Product = g.Key,
                                    Count = g.Count()
                       });

这篇关于如何使用LINQ从父母列表中选择和组复杂的子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆