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

  • Question

  • 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


    • Edited by wingers Saturday, October 10, 2020 12:16 PM
    Saturday, October 10, 2020 12:16 PM

All replies

  • Hi

    Mostly, there is no 'wrong' way to get a result.  If it works, then it is good.  The only thing is time to get the result - and for that, in my experience, LINQ is often slower despite it looking cute and compact etc.

    My suggestion, and something I do at times, is to set up a StopWatch and time each method with real world example data.  That onl means you get the oprimum from the methods you choose to est and there may still be other untested methods which are faster.


    Regards Les, Livingston, Scotland

    Saturday, October 10, 2020 12:27 PM
  • @Leshay

    Thanks for reply - yes that makes perfect sense and some other articles did seem to suggest LINQ not always the quickest method

    I will do some timings and see


    Darren Rose

    Saturday, October 10, 2020 12:30 PM
  • Looks to me you read results with Linq twice. It was done with the Linq Where clause you then  did a Sum() on the results from the Linq Where clause. You can do the Sum() when doing the Linq Where clause statement eliminating the second Linq statement doing a Sum() on the result from the first Linq statement.



    • Proposed as answer by DA924x Saturday, October 10, 2020 12:52 PM
    • Unproposed as answer by DA924x Saturday, October 10, 2020 12:53 PM
    • Edited by DA924x Saturday, October 10, 2020 12:54 PM
    Saturday, October 10, 2020 12:51 PM
  • @DA924x

    Thanks, very useful, now amended to

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



    Darren Rose

    Saturday, October 10, 2020 1:27 PM
  • @DA924x

    Thanks, very useful, now amended to

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



    In my opinion, the first statement (Dim results = nodes.Where(Function(n) n.FullName.StartsWith("YELLOW"))) does not have an effect until you call something like Sum or ToList. It is not useful to amend the code in this case.

    From the documentation for Where: “This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated […]”


    Saturday, October 10, 2020 6:18 PM