Best method to get information from an IEnumerable - LINQ or For Each or ?

  • Thread starter Thread starter wingers
  • Start date Start date
W

wingers

Guest
Hi

Hoping for some guidance on the best / correct / fastest way to get required information from my IEnumerable

Basically the IEnumerable (called nodes) contains various properties but I am only at this point interested in two of them - NAME which is a string and SIZE which is a ulong

I want to get a total (sum) of the SIZE when the NAME equals or startswith a specific value

So for example if NAME.startswith("YELLOW") then get SIZE from all matches and return it as a total (SUM)

I have it working using both a For..Each and LINQ? but need to know if I am doing this in best way, or whether it should be approached differently

For..Each

Dim size As Long = 0
For Each x In nodes
If x.FullName.StartsWith("YELLOW") Then
size = size + x.Size
End If
Next

LINQ?

Dim results = nodes.Where(Function(n) n.FullName.StartsWith("YELLOW"))
Dim totalsum = results.Sum(Function(n) n.Size)


Any comments / advice on this will be gratefully received

Thanks






Darren Rose

Continue reading...
 
Back
Top