linq - 通过 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
}
]
}
]
解决方案
如果我理解正确,您可以使用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);
我希望你觉得这有帮助。
推荐阅读
- javascript - vuejs动态组件为v-bind抛出错误:is,错误是属性或方法...未在实例上定义但在渲染期间引用
- python - 我如何将 Python 中的应用程序转换为在 Android 中运行
- bash - Bash将变量值从for循环传递到嵌套for循环
- c++ - 视觉工作室 2017;启用 /permissive 时,“const wchar_t *”类型的参数与“PWSTR”类型的参数不兼容
- python - Python:将 MxN 数组更改为 NxM
- c++ - 使用模板参数重载 C++ 方法:如何使其适用于模板的子类?
- logging - 如何还将 kotlin-logging 日志(sl4j-simple?)发送到我自己的后端
- c# - 有没有办法在评论中提及一行?
- git - 如何解决 GitLab 上的冲突?
- jquery - Jquery 动画工作不一致