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

前端 未结 5 1016
遇见更好的自我
遇见更好的自我 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: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

提交回复
热议问题