LINQ : Query A Class

The
following code creates a generic list based on a custom class and
then performs a LINQ query:
//Declare List Object
public static List<GPRider> riderList;

//Declare Class Object
public class GPRider
{
    public int Number { getset; }
    public string FirstName { getset; }
    public string LastName { getset; }
    public string Team { getset; }
    public string BikeMake { getset; }
    public string BikeModel { getset; }
}

//Create Data To Be Used
public static void createRiders()
{
    riderList = new List<GPRider>{
    new GPRider {Number=46, FirstName=“Valentino”, LastName=“Rossi”
        Team=“Factory Yamaha”, BikeMake=“Yamaha”, BikeModel=“M1”},
    new GPRider {Number=99, FirstName=“Jorge”, LastName=“Lorenzo”
        Team=“Factory Yamaha”, BikeMake=“Yamaha”, BikeModel=“M1”},
    new GPRider {Number=69, FirstName=“Nicky”, LastName=“Hayden”
        Team=“Factory Ducati”, BikeMake=“Ducati”, BikeModel=“DesmoGP13”},
    new GPRider {Number=35, FirstName =“Cal”, LastName=“Crutchlow”
        Team=“Tech 3”, BikeMake=“Yamaha”, BikeModel=“M1”}
    };
}
//Finally, LINQ Query Example For Generic List
public static void RiderQuery()
{
    //populate the Generic List
    createRiders();

    //create the LINQ query
    var linqQuery =
        from r in riderList
        select new { r.FirstName, r.LastName, r.Number, r.Team, 
            r.BikeMake, r.BikeModel };

    //Loop through LINQ query results
    foreach (var rider in linqQuery)
    {
        Console.WriteLine(“{0} {1}, #{2}, rides a {3} {4}, for the {5} team.”
            rider.FirstName, rider.LastName, rider.Number, rider.BikeMake, 
            rider.BikeModel, rider.Team);
    }

    Console.ReadLine();

}

LINQ : Query A Simple Array

Query String Array:
public static void ExecuteBasicStringArray()
{
    //create string array
    string[] bikes = new string[] { “Yamaha”“Honda”“Ducati”“Suzuki” };


  //build LINQ query
    var queryStrings =
        from bike in bikes
        where bike == “Honda” || bike == “Yamaha”
        select bike;


  //view results of query
    foreach (var b in queryStrings)
    {
        Console.WriteLine(b);
    }

    Console.ReadLine();

}
Query Integer Array:
public static void ExecuteBasicIntArray()
{
   //create integer array
    int[] numbers = { 46, 99, 69, 35 };


  //build LINQ query
    var queryNums =
        from num in numbers
        where num < 99
        select num;


  //view results of query
    foreach (var n in queryNums)
    {
        Console.WriteLine(n);
    }

    Console.ReadLine();

}

LINQ : An Introduction

LINQ is an acronym for “Language
Integrated Query.” It’s been around since Visual Studio 2008 but
in the works far in advance of that; however, many companies are just
now using it for the first time. It is a programming model
developed by Microsoft to allow developers to query several types of
objects as if they were querying a SQL database. For example,
developers can now query for a textbox on a form with the ease of
querying for a row in a database table – often, with less overhead.

The syntax is similar to SQL except
that the clauses are ordered differently. For example, note the
following LINQ query:
var queryProduct =
from p in products
select p.ProductName;

Common objects queryable by LINQ are
enumerable classes, arrays, datasets, and XML documents.