How to count the number of elements that match a condition with LINQ

前端 未结 5 989
遇见更好的自我
遇见更好的自我 2020-11-29 12:09

I\'ve tried a lot of things but the most logical one for me seems this one:

int divisor = AllMyControls.Take(p => p.IsActiveUserControlChecked).Count();
<         


        
相关标签:
5条回答
  • 2020-11-29 12:28

    Why not directly use Count? That == true statement is also highly redundant.

    int divisor = AllMyControls.Count(p => p.IsActiveUserControlChecked);
    

    Also, you are getting an error on your Take method because it is waiting for an int. You need to specify the number of contiguous elements from the start of the collection you want to get, you cannot put a lambda expression. You need to use TakeWhile for that. So

    int divisor = AllMyControls.TakeWhile(p => p.IsActiveUserControlChecked == true).Count();
    

    would have been correct, but would not work the way you expect it; it stops once the condition is broken. So if AllMyControls contains true, true, false, true, TakeWhile with Count will return 2 instead of your expected 3.

    0 讨论(0)
  • 2020-11-29 12:29

    The parameter for Take requres an int and you are passing in a delegate/ lambda expression. Take is designed to just take the first count of the elements.

    You can use the Count method and pass in a delegate to count the elements that fit its criteria. This way you only iterate the IEnumerable once, rather than first culling out the ones that don't fit your criteria, and then again to actually count them.

    AllMyControls.Count(p => p.IsActiveUserControlChecked);
    
    0 讨论(0)
  • 2020-11-29 12:40
    int divisor = AllMyControls.Where(p => p.IsActiveUserControlChecked).Count()
    

    or simply

    int divisor = AllMyControls.Count(p => p.IsActiveUserControlChecked);
    

    Since you are a beginner, it would be worthwhile to take a look at Enumerable documentation

    0 讨论(0)
  • 2020-11-29 12:41

    Try

    int divisor = AllMyControls.Where(x => x.IsActiveUserControlChecked == true).Count();
    
    0 讨论(0)
  • 2020-11-29 12:44

    Do not KISS

    int divisor = AllMyControls.Count(p => p.IsActiveUserControlChecked);
    
    0 讨论(0)
提交回复
热议问题