从 属性 IEnumerable 之间的比较将 属性 添加到 IEnumerable
Add property to an IEnumerable from property comparison between IEnumerable's
我有一个 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
class 我在上面读取数据的地方:
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
}
]
}
]
在域中,我有以下 classes 来为服务映射数据:
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; }
}
}
以及下面的 classes,我在这里进行数据的分组和加载:
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
}
]
}
]
如果我没理解错的话,你可以使用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);
希望对您有所帮助。
我有一个 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
class 我在上面读取数据的地方:
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
}
]
}
]
在域中,我有以下 classes 来为服务映射数据:
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; }
}
}
以及下面的 classes,我在这里进行数据的分组和加载:
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
}
]
}
]
如果我没理解错的话,你可以使用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);
希望对您有所帮助。