A simple SELECT query in Linq

static void Main(string[] args)
{
    string[] cars = { "VW Golf", 
                        "Opel Astra", 
                        "Audi A4", 
                        "Ford Focus", 
                        "Seat Leon", 
                        "VW Passat", 
                        "VW Polo", 
                        "Mercedes C-Class" };

    var list = from car in cars
               select car;

    StringBuilder sb = new StringBuilder();

    foreach (string entry in list)
    {
        sb.Append(entry + "\\n");
    }

    Console.WriteLine(sb.ToString());
    Console.ReadLine();
}

In the example above, an array of strings (cars) is used as a collection of objects to be queried using LINQ. In a LINQ query, the from clause comes first in order to introduce the data source (cars) and the range variable (car). When the query is executed, the range variable will serve as a reference to each successive element in cars. Because the compiler can infer the type of car, you do not have to specify it explicitly

When the above code is compiled and executed, it produces the following result:

https://i.stack.imgur.com/lG65Q.png

SELECT with a WHERE Clause

var list = from car in cars
           where car.Contains("VW")
           select car;

The WHERE clause is used to query the string array (cars) to find and return a subset of array which satisfies the WHERE clause.

When the above code is compiled and executed, it produces the following result:

https://i.stack.imgur.com/llGXx.png

Generating an Ordered List

var list = from car in cars
           orderby car ascending 
           select car;

Sometimes it is useful to sort the returned data. The orderby clause will cause the elements to be sorted according to the default comparer for the type being sorted.

When the above code is compiled and executed, it produces the following result:

https://i.stack.imgur.com/ODH55.png

Working with a custom type

In this example, a typed list is created, populated, and then queried

public class Car
{
    public String Name { get; private set; }
    public int UnitsSold { get; private set; }

    public Car(string name, int unitsSold)
    {
        Name = name;
        UnitsSold = unitsSold;
    }
}

class Program
{
    static void Main(string[] args)
    {

        var car1 = new Car("VW Golf", 270952);
        var car2 = new Car("Opel Astra", 56079);
        var car3 = new Car("Audi A4", 52493);
        var car4 = new Car("Ford Focus", 51677);
        var car5 = new Car("Seat Leon", 42125);
        var car6 = new Car("VW Passat", 97586);
        var car7 = new Car("VW Polo", 69867);
        var car8 = new Car("Mercedes C-Class", 67549);

        var cars = new List<Car> { 
            car1, car2, car3, car4, car5, car6, car7, car8 };
        var list = from car in cars
                   select car.Name;

        foreach (var entry in list)
        {
            Console.WriteLine(entry);
        }
        Console.ReadLine();
    }
}

When the above code is compiled and executed, it produces the following result:

https://i.stack.imgur.com/0jUOC.png