Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Generic List - Count (where)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Generic List - Count (where)

    All,

    Hoping some of the senior members may be able to assist...

    I've a generic list populated by doubles each bound between 0 & 1. To map the distribution of elements between 0 & 1, I'm trying to count the number of elements < 0.1, then count the number of elements < 0.2, then < 0.3 etc.

    I originally thought the Count() method would be the way to go... but have no idea how to apply it to a single list.
    http://msdn.microsoft.com/en-us/library/bb535181.aspx

    As always any assistance would be appreciated.
    Regards
    Shannon

    #2
    Shannon, from a quick look - wouldn't FindAll be a start for your tasks?

    Retrieves all the elements that match the conditions defined by the specified predicate.

    Comment


      #3
      Bertrand,

      Thanks for the solid guidance.

      Another question, a Count of the TheList.FindAll(item => item <= 0.5) yields the number of elements in the list that satisfy the requirement (item <= 0.5). Brilliant. And, as expected, TheList.Count is the total number of elements in The.List.

      I've tested these two parts separately, however, when combined to give the percentage of elements that satisfy the requirement (below) it all goes awry. It returns a zero or a one, as opposed to some number bound by zero and one.
      Code:
      TheList.FindAll(item => item <= 0.5).Count / TheList.Count
      As Always any ideas would be appreciated.
      Regards
      Shannon

      Comment


        #4
        Hi Shannon, I would tip on a division or precision issue here - could that be the case?

        Comment


          #5
          Your operands are integers: cast them to doubles first, before you do the division. Also check for a zero divisor.

          Comment


            #6
            koganam,

            You nailed it!
            The divisor was an integer. Cast to a double it works the treat.

            Thanks again
            Shannon

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            581 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            338 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            103 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            554 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            552 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X