Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Getting count from a LINQ query in C#

Example:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace ForgetCode
  7. {
  8. class IntroToLINQ
  9. {
  10. static void Main()
  11. {
  12. // The Three Parts of a LINQ Query:
  13. // 1. Data source.
  14. int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };
  15.  
  16. // 2. Query creation.
  17. // numQuery is an IEnumerable<int>
  18. var numQuery =
  19. from num in numbers
  20. where (num % 2) == 0
  21. select num;
  22.  
  23. // 3. Query execution.
  24. int evenNumCount = numQuery.Count();
  25.  
  26. Console.WriteLine(evenNumCount);
  27.  
  28. }
  29. }
  30. }


Output:
  1. 4