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 sjsj2732, Yesterday, 04:31 AM
            0 responses
            38 views
            0 likes
            Last Post sjsj2732  
            Started by NullPointStrategies, 03-13-2026, 05:17 AM
            0 responses
            287 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            288 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            134 views
            1 like
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            95 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Working...
            X