Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. What's more efficient in .Any and .Count in C# (Extension methods)

What's more efficient in .Any and .Count in C# (Extension methods)

Scheduled Pinned Locked Moved C#
csharpquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rosharavinda
    wrote on last edited by
    #1

    public void MethodName(ObservableCollection<DataCollection> dataCollection)
    {
    if (dataCollection != null)
    {
    IsChecked = dataCollection.Any(o => o.DataCollectionID.Equals(30));

            IsChecked = dataCollection.Where(o => o.DataCollectionID.Equals(30)).Count() > 0;
        }
    }
    

    Can anyone explain me, what could be the most efficient way to use from above two filtering? .Any? or .Where.Count?

    Note: Consider that dataCollection has over 10,000 items.

    Please advice me. Thank you

    P OriginalGriffO N 3 Replies Last reply
    0
    • R rosharavinda

      public void MethodName(ObservableCollection<DataCollection> dataCollection)
      {
      if (dataCollection != null)
      {
      IsChecked = dataCollection.Any(o => o.DataCollectionID.Equals(30));

              IsChecked = dataCollection.Where(o => o.DataCollectionID.Equals(30)).Count() > 0;
          }
      }
      

      Can anyone explain me, what could be the most efficient way to use from above two filtering? .Any? or .Where.Count?

      Note: Consider that dataCollection has over 10,000 items.

      Please advice me. Thank you

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      The Any clause should be faster because it can run without an iterator (think yield return here), so it stops when it hits the first true condition, i.e. your collection id equaling 30. The Where clause provides an iterator that is driven by the count, so this must complete the iteration - the actual condition of checking for a count greater than 0 happens after the Where clause finishes evaluating.

      1 Reply Last reply
      0
      • R rosharavinda

        public void MethodName(ObservableCollection<DataCollection> dataCollection)
        {
        if (dataCollection != null)
        {
        IsChecked = dataCollection.Any(o => o.DataCollectionID.Equals(30));

                IsChecked = dataCollection.Where(o => o.DataCollectionID.Equals(30)).Count() > 0;
            }
        }
        

        Can anyone explain me, what could be the most efficient way to use from above two filtering? .Any? or .Where.Count?

        Note: Consider that dataCollection has over 10,000 items.

        Please advice me. Thank you

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        And to add to what Pete says, if you need to know whichis the most efficient, it's simple enough to time it. Use the Stopwatch class[^] and run a good number of times to eliminate external variance.

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • R rosharavinda

          public void MethodName(ObservableCollection<DataCollection> dataCollection)
          {
          if (dataCollection != null)
          {
          IsChecked = dataCollection.Any(o => o.DataCollectionID.Equals(30));

                  IsChecked = dataCollection.Where(o => o.DataCollectionID.Equals(30)).Count() > 0;
              }
          }
          

          Can anyone explain me, what could be the most efficient way to use from above two filtering? .Any? or .Where.Count?

          Note: Consider that dataCollection has over 10,000 items.

          Please advice me. Thank you

          N Offline
          N Offline
          Nitzan Levi
          wrote on last edited by
          #4

          Hi, They are both right. Just to be more accurate Where() does not necessarily enumerate over all elements, that depends on what you put in the condition. In general the advice is to stick to Any().

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups