How to use linq in c#?

LINQ allows querying collections in a declarative manner.

Example:


  List numbers = new List {1, 2, 3, 4, 5};
  var evens = numbers.Where(n => n % 2 == 0);
  

Solution:


  foreach (var even in evens)
  {
      Console.WriteLine(even);
  }
  

Beginner's Guide to C#