首页 > 解决方案 > 通过 IEnumerable 之间的属性比较将属性添加到 IEnumerable

问题描述

我有一个 webapi,它从 MySQL 中的两个不同模式读取数据,customers并且simuladoMurex. 通过读取客户模式,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<ReportCustomerKey>并将该counterparty字段与该IEnumerable<Customers> customer字段进行比较。当这些字段相同时,我需要将 的email属性Customers添加到IEnumerable<ReportCustomerKey>. 我很难想象我怎么能做到这一点,有人可以帮忙吗?输出示例:

[
    {
        "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-coreef-core-3.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);

我希望你觉得这有帮助。


推荐阅读