using System; using System.Linq; using System.Collections.Generic; namespace ClassExamples { class Car { public int Year { get; set; } } class Tester { public static void Main() { Car[] cars = { new Car {Year = 1997}, new Car {Year = 1998}, new Car {Year = 2002}, new Car {Year = 2002}, new Car {Year = 2003}, new Car {Year = 2004}, new Car {Year = 2004}, new Car {Year = 2005}, new Car {Year = 2006}, new Car {Year = 2007}, new Car {Year = 2008}, new Car {Year = 2009} }; IEnumerable queryResult; int year = 2003; queryResult = oldWay(cars, year); //queryResult = withLinq(cars, year); //queryResult = withLinqStyle2(cars, year); Console.WriteLine(queryResult.Count() + " cars are made after " + year); } public static IEnumerable oldWay (Car[] cars, int queryYear) { List results = new List(); foreach (Car c in cars) { if (c.Year > queryYear) { results.Add(c); } } return results; } public static IEnumerable withLinq (Car[] cars, int queryYear) { return from car in cars where car.Year > queryYear select car; } public static IEnumerable withLinqStyle2 (Car[] cars, int queryYear) { return cars.Where(car => car.Year > queryYear).Select(car => car); } } }