【发布时间】:2020-10-20 19:11:45
【问题描述】:
我有一个 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 字段进行比较。当这些字段相同时,我需要将Customers 的email 属性添加到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-core ef-core-3.1