[英]How to write this nested foreach loop query in LINQ?
你能幫我在 LINQ 中編寫這個嵌套的 foreach 循環嗎?
c=0
foreach(var e in elements)
{
foreach(var a in e.Attributes)
{
if(a.Name=="City" && a.GetValue().ToString() == "Oakland")
c += 1;
}
}
它應該是這樣的(使用SelectMany
和Count
方法):
int c = elements.SelectMany(e => e.Attributes)
.Count(a => a.Name == "City" && a.GetValue().ToString() == "Oakland");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.