假设有一个person对象有很多ICollection而ObjectType2有Icollection
因此,要编辑属性,理论上您可以使用forloops进行深入搜索.但是什么是语法上更好的更好的方法.
例如,要编辑名为PP的属性,可以执行以下操作:
foreach (var item in PersonData)
{
foreach (var item2 in item.Staffs)
{
foreach (var item3 in item2.Employees)
{
foreach (var item4 in item3.EmployeePositions)
{
item4.PP = "test";
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我正在寻找一些非常好的东西,例如:无论是通过linq还是其他方法.
var positions = PersonData
.SelectMany(p => p.Staffs)
.SelectMany(s => s.Employees)
.SelectMany(e => e.EmployeePositions);
foreach (var position in positions)
{
position.PP = "test";
}
Run Code Online (Sandbox Code Playgroud)
这相当于嵌套循环.
| 归档时间: |
|
| 查看次数: |
65 次 |
| 最近记录: |