简体   繁体   中英

How to write this nested foreach loop query in LINQ?

Can you please help me write this nested foreach loop in LINQ?

c=0
foreach(var e in elements)
{  
     foreach(var a in e.Attributes)
     {
          if(a.Name=="City" && a.GetValue().ToString() == "Oakland")                                                 
                 c += 1;                    
     }
}

It should be something like this (Use SelectMany and Count methods):

int c = elements.SelectMany(e => e.Attributes)
                .Count(a => a.Name == "City" && a.GetValue().ToString() == "Oakland");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM