好日子,
假设我有一个静态List<AClass>对象(让我们将其命名为myStaticList),其中包含其他列表,该列表包含具有CId和Name属性的其他列表.
我需要做的是
foreach(AClass a in myStaticList)
{
foreach(BClass b in a.bList)
{
foreach(CClass c in b.cList)
{
if(c.CId == 12345)
{
c.Name = "Specific element in static list is now changed.";
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我可以使用LINQ Lambda表达式实现此目的吗?
就像是;
myStaticList
.Where(a=>a.bList
.Where(b=>b.cList
.Where(c=>c.CId == 12345) != null) != null)
.something logical
.Name = "Specific element in static list is now changed.";
Run Code Online (Sandbox Code Playgroud)
请注意,我想更改静态列表中该特定项的属性.
您需要SelectMany展平您的列表:
var result = myStaticList.SelectMany(a=>a.bList)
.SelectMany(b => b.cList)
.FirstOrDefault(c => c.CId == 12345);
if(result != null)
result.Name = "Specific element in static list is now changed.";;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5517 次 |
| 最近记录: |