如何将两个平面列表组合成一个嵌套对象 [英] How to combine two flat lists into one nested object

查看:89
本文介绍了如何将两个平面列表组合成一个嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个列表,每个列表中包含三个相同的属性.我想将结果合并到一个集合中.防爆类的结构如下

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

public class Order
{
    public int ProductId { get; set; }
    public int CustomerId { get; set; }
    public int OrderId { get; set; }

    // Few other Properties of OrderDetail
}
public class PaymentDetail
{
    public int ProductId { get; set; }
    public int CustomerId { get; set; }
    public int OrderId { get; set; }
    // Few other Properties form PaymentDetail
}

public class CouponUsageDetail
{
    public int ProductId { get; set; }
    public int CustomerId { get; set; }
    public int OrderId { get; set; }
    // Few other Properties form CouponUsageDetail
}

这种类型的输出来自一个API服务,其中每个类都采用列表对象(JSON格式)的形式,我们需要对此执行一些操作.所有三个属性(ProductId,CustomerId,OrderId)在每个集合中都包含相同的值,这意味着在每个集合中都重复了这三个属性.我们需要对这些集合进行一些查找.因此,通常情况下,我们可以这样处理-从订单"列表开始foreach并过滤PaymentDetail和CouponUsageDetail的所有三个匹配属性.但是,当数据大小增加时,在性能方面会更昂贵.因此,考虑将嵌套结构预先放置并避免查找.如果我们将输出嵌套如下,这将有助于避免在PaymentDetail和CouponUsageDetail上进行查找. 举例-我们收到以下格式的JOSN

This type of output is coming from one API service where each class is in form of list object (JSON format) and we need to perform some operations on this. All three properties (ProductId,CustomerId, OrderId) contains same values in each collection which means all three properties are repeated in each collection. We need to perform some look ups on these collections. So in normal way what we can do with it is as - start a foreach from Order list and filter all three matching properties of PaymentDetail and CouponUsageDetail. But it would be costlier in term of performance when the data size is increased. So thought of making nesting structure upfront and avoid lookups. If we make the output nested as below this will help to avoid lookups on PaymentDetail and CouponUsageDetail. Ex - we are receiving JOSN in below format

{"Orders":[{"ProductId":301,"CustomerId":101,"OrderId":201},{"ProductId":701,"CustomerId":501,"OrderId":601}],"PaymentDetails":[{"ProductId":301,"CustomerId":101,"OrderId":201},{"ProductId":701,"CustomerId":501,"OrderId":601}],"CouponUsageDetails":[{"ProductId":301,"CustomerId":101,"OrderId":201},{"ProductId":701,"CustomerId":501,"OrderId":601}]}

,通过此输出,我们希望将对象形成为

and with this output we want to form object as

public class OrderDetails
{
    public int ProductId { get; set; }
    public int CustomerId { get; set; }
    public int OrderId { get; set; }
    // Few other Properties of OrderDetail

    List<PaymentDetail> PaymentDetail { get; set; }
    List<CouponUsageDetail> CouponUsageDetail { get; set; }
}

您能指导linq的最佳最佳使用方法是什么,我们可以将所有这三个匹配的属性组合在一起并使其更好地只是一个嵌套结构? 谢谢你! 注意:我知道此结构需要规范化,但是由于我们不在控制范围之内,因此请在此处忽略规范化规则.

Can you guide, what would be the best optimum usage of linq where we can combine all these three matching properties and make it just one nested structure better? Thank You! Note: I know this structure needs to be normalized but please ignore the normalization rule here as this is not in our control.

推荐答案

您在描述什么,就像两个标准的多键LINQ

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();

这篇关于如何将两个平面列表组合成一个嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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