如何根据 C# 中的条件组合具有不同属性的两个列表
How to combine two lists with different properties based on a condition in C#
我有两个列表 - 具有不同属性的计划和部门..
示例数据:
{
"plans": [
{
"planCode": "A",
"planShortName": "Apple",
"planType": null,
"bisPlanCode": "878",
},
{
"planCode": "B",
"planShortName": "Ball",
"planType": null,
"bisPlanCode": "536",
}
],
"divisions": [
{
"planCode": "878",
"divisions": [
{ divisionCode: "2", divisionName: "test2" },
{ divisionCode: "1", divisionName: "test1" }]
},
{
"planCode": "536",
"divisions": [
{ divisionCode: "3", divisionName: "test3" },
{ divisionCode: "1", divisionName: "test1" }
]
}
]
}
如何在 C# 中 plans.bisPlanCode == divisions.plansCode 的条件下将这两个列表-计划和分区组合成一个列表 planDivisions。
所以最终结果应该是
"planDivisions": [
{
"planCode": "A",
"planShortName": "Apple",
"planType": null,
"bisPlanCode": "878",
"divisions": [ { divisionCode: "2", divisionName: "test2" },
{ divisionCode: "1", divisionName: "test1" }]
},
{
"planCode": "B",
"planShortName": "Ball",
"planType": null,
"bisPlanCode": "536",
"divisions": [
{ divisionCode: "3", divisionName: "test3" },
{ divisionCode: "1", divisionName: "test1"
}]
}
]
我尝试过的事情:
List<Data> planDivisions = new List<Data>();
Divisions.ForEach(division =>
{
var plan = Plans.Find(p => p.PlanCode == division.PlanCode);
if (plan != null)
{
Data data = new Data();
data.DivisionData = division;
data.PlanData = plan;
planDivisions.Add(data);
}
});
有什么有效的方法吗?
您可以使用 LINQ 连接两个数据列表。更多信息 here.
class Plan {
public int PlanCode { get; set; }
public string PlanShortName { get; set; }
}
class Division {
public int DivisionCode { get; set; }
public string DivisionName { get; set; }
}
class DivisionAssignment {
public int PlanCode { get; set; }
public List<Division> Divisions { get; set; }
}
public static void JoinExample() {
// do your deserialization stuff here
List<Plan> plans = ...;
List<DivisionAssignment> assignments = ...;
// join the data
var query = from plan in plans
join assignment in assignments on plan.PlanCode equals assignment.PlanCode
select new { PlanCode = plan.PlanCode, Divisions = assignment.Divisions };
// reach the joined data
foreach (var planDivision in query) {
... = planDivision.PlanCode;
... = planDivision.Divisions;
...
}
}
我有两个列表 - 具有不同属性的计划和部门.. 示例数据:
{
"plans": [
{
"planCode": "A",
"planShortName": "Apple",
"planType": null,
"bisPlanCode": "878",
},
{
"planCode": "B",
"planShortName": "Ball",
"planType": null,
"bisPlanCode": "536",
}
],
"divisions": [
{
"planCode": "878",
"divisions": [
{ divisionCode: "2", divisionName: "test2" },
{ divisionCode: "1", divisionName: "test1" }]
},
{
"planCode": "536",
"divisions": [
{ divisionCode: "3", divisionName: "test3" },
{ divisionCode: "1", divisionName: "test1" }
]
}
]
}
如何在 C# 中 plans.bisPlanCode == divisions.plansCode 的条件下将这两个列表-计划和分区组合成一个列表 planDivisions。 所以最终结果应该是
"planDivisions": [
{
"planCode": "A",
"planShortName": "Apple",
"planType": null,
"bisPlanCode": "878",
"divisions": [ { divisionCode: "2", divisionName: "test2" },
{ divisionCode: "1", divisionName: "test1" }]
},
{
"planCode": "B",
"planShortName": "Ball",
"planType": null,
"bisPlanCode": "536",
"divisions": [
{ divisionCode: "3", divisionName: "test3" },
{ divisionCode: "1", divisionName: "test1"
}]
}
]
我尝试过的事情:
List<Data> planDivisions = new List<Data>();
Divisions.ForEach(division =>
{
var plan = Plans.Find(p => p.PlanCode == division.PlanCode);
if (plan != null)
{
Data data = new Data();
data.DivisionData = division;
data.PlanData = plan;
planDivisions.Add(data);
}
});
有什么有效的方法吗?
您可以使用 LINQ 连接两个数据列表。更多信息 here.
class Plan {
public int PlanCode { get; set; }
public string PlanShortName { get; set; }
}
class Division {
public int DivisionCode { get; set; }
public string DivisionName { get; set; }
}
class DivisionAssignment {
public int PlanCode { get; set; }
public List<Division> Divisions { get; set; }
}
public static void JoinExample() {
// do your deserialization stuff here
List<Plan> plans = ...;
List<DivisionAssignment> assignments = ...;
// join the data
var query = from plan in plans
join assignment in assignments on plan.PlanCode equals assignment.PlanCode
select new { PlanCode = plan.PlanCode, Divisions = assignment.Divisions };
// reach the joined data
foreach (var planDivision in query) {
... = planDivision.PlanCode;
... = planDivision.Divisions;
...
}
}