【问题标题】:Add property to an IEnumerable from property comparison between IEnumerable's通过 IEnumerable 之间的属性比较将属性添加到 IEnumerable
【发布时间】:2020-10-20 19:11:45
【问题描述】:

我有一个 webapi,它从 MySQL 中的两个不同模式读取数据,customerssimuladoMurex。通过读取客户架构,webapi 在服务器上提供了一个 json,如下所示:

[
    {
        "customer": "Itau",
        "email": "itau@hotmail.com"
    },
    {
        "customer": "Jordan Banks",
        "email": "jordan@hotmail.com"
    },
    {
        "customer": "Santander",
        "email": "santander@hotmail.com"
    }
]

我有 customers 类,我在其中读取了上面的数据:

using System.Collections.Generic;
using System.Linq;

namespace SimuladoMurex.Domain.Entities.Customers
{
    public class Customers
    {
        public string Customer { get; set; }
        public string Email { get; set; }

        public IEnumerable<Customers> LoadData(IEnumerable<Customers> data)
        {
            return data.Select(x => new Customers
            {
                Customer = x.Customer,
                Email = x.Email
            }).ToArray();
        }
    }
}

通过阅读simuladoMurex,我有以下json:

[
    {
        "counterparty": "Santander",
        "rc": [
            {
                "tradeDate": "2020-05-23T10:03:12",
                "isin": "DOL110",
                "typology": 0
            },
            {
                "tradeDate": "2020-06-11T11:24:08",
                "isin": "LIT300",
                "typology": 0
            }
        ]
    },
    {
        "counterparty": "Jordan Banks",
        "rc": [
            {
                "tradeDate": "2020-06-11T11:23:22",
                "isin": "LIT250",
                "typology": 0
            },
            {
                "tradeDate": "2020-06-11T18:08:44",
                "isin": "CIT450",
                "typology": 0
            }
        ]
    },
    {
        "counterparty": "Itau",
        "rc": [
            {
                "tradeDate": "2020-06-11T18:06:53",
                "isin": "LIT350",
                "typology": 0
            },
            {
                "tradeDate": "2020-06-11T18:07:10",
                "isin": "LIT450",
                "typology": 0
            }
        ]
    }
]

在域中,我有以下类来映射服务的数据:

using System;

namespace SimuladoMurex.Domain.Entities
{
    public class Mo
    {
        public int MoId { get; set; }
        public DateTime TradeDate { get; set; }
        public string Counterparty { get; set; }     

        public Ir Ir { get; set; }
    }

    public class Ir
    {
        public int MoId { get; set; }
        public string Isin { get; set; }
        public decimal Price { get; set; }
        public int Trader { get; set; }

        public virtual Mo Mo { get; set; }
    }
}

以及下面的类,我在其中对数据进行分组和加载:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimuladoMurex.Domain.Entities.Reports
{
    public class ReportCustomers
    {
        public DateTime TradeDate { get; set; }
        public string Isin { get; set; }
        public int Typology { get; set; 
        }
        
    }

    public class ReportCustomerKey
    {
        public string Counterparty { get; set; }
        public IEnumerable<ReportCustomers> rc { get; set; }

        public IEnumerable<ReportCustomerKey> LoadData(IEnumerable<Mo> data)
        {
            return data.GroupBy(x => x.Counterparty)
                       .Select(x => new ReportCustomerKey
                       {
                           Counterparty = x.Key,
                           rc = x.Select(i => new ReportCustomers
                           {
                               TradeDate = i.TradeDate,
                               Isin = i.Ir.Isin
                           }).ToList()
                       });
        }
    }
}

我需要遍历 IEnumerable&lt;ReportCustomerKey&gt; 并将 counterparty 字段与 IEnumerable&lt;Customers&gt; customer 字段进行比较。当这些字段相同时,我需要将Customersemail 属性添加到IEnumerable&lt;ReportCustomerKey&gt;。我很难想象我怎么能做到这一点,有人可以帮忙吗?输出示例:

[
    {
        "counterparty": "Santander",
        "email": "santander@hotmail.com",
        "rc": [
            {
                "tradeDate": "2020-05-23T10:03:12",
                "isin": "DOL110",
                "typology": 0
            },
            {
                "tradeDate": "2020-06-11T11:24:08",
                "isin": "LIT300",
                "typology": 0
            }
        ]
    },
    {
        "counterparty": "Jordan Banks",
        "email": "jordan@hotmail.com"
        "rc": [
            {
                "tradeDate": "2020-06-11T11:23:22",
                "isin": "LIT250",
                "typology": 0
            },
            {
                "tradeDate": "2020-06-11T18:08:44",
                "isin": "CIT450",
                "typology": 0
            }
        ]
    },
    {
        "counterparty": "Itau",
        "email": "itau@hotmail.com"
        "rc": [
            {
                "tradeDate": "2020-06-11T18:06:53",
                "isin": "LIT350",
                "typology": 0
            },
            {
                "tradeDate": "2020-06-11T18:07:10",
                "isin": "LIT450",
                "typology": 0
            }
        ]
    }
]

【问题讨论】:

    标签: linq .net-core ef-core-3.1


    【解决方案1】:

    如果我理解正确,您可以使用Join 根据匹配键(Counterparty,Customer)获取常用数据,如以下代码:

    1 - 将Email 属性添加到ReportCustomerKey

    public class ReportCustomerKey
    {
        public string Counterparty { get; set; }
    
        public string Email { get; set; }
    
        public IEnumerable<ReportCustomers> rc { get; set; }
    .....
    }
    

    2 - 使用Join 喜欢:

    IEnumerable<Customers> customers;
    IEnumerable<ReportCustomerKey>;
    
    List<ReportCustomerKey> result = reportCustomers.Join(customers,
        rc => rc.Counterparty,
        c => c.Customer,
        (rc, c) =>
        new ReportCustomerKey
        {
            Counterparty = rc.Counterparty,
            Email = c.Email,
            rc = rc.rc
        }).ToList();
    

    3 - 演示:

    string json = JsonConvert.SerializeObject(result);
    Console.WriteLine(json);
    

    希望对您有所帮助。

    【讨论】:

    • 谢谢,很有用!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 2020-12-14
    相关资源
    最近更新 更多