1.2. A Simple C# 3.0 LINQ to Objects Program

Listing 1-1 is a console program snippet that uses LINQ to Objects to display a specific element in an array.

Example 1-1. Using LINQ to Objects with List<T>
List<Person> people = new List<Person> {
    new Person() { ID = 1,
                   IDRole = 1,
                   LastName = "Anderson",
                   FirstName = "Brad"},

new Person() { ID = 2,
                   IDRole = 2,
                   LastName = "Gray",
                   FirstName = "Tom"}
            };

            var query = from p in people
                        where p.ID == 1
                        select new { p.FirstName, p.LastName };

            ObjectDumper.Write(query);

In Listing 1-1 you define a collection of Person objects and insert a couple of elements. List<T> is a generic class that implements IEnumerable<T>, so it's suitable for LINQ querying.

Next you declare a variable, query, to hold the result of a LINQ query. Don't worry about the var keyword right now; it will be discussed later in this chapter, in "Implicitly Typed Local Variables."

You initialize query to a LINQ's query expression. The from clause specifies a data source. The variable p represents an object in the people collection. The where clause specifies a condition for selecting from the data source. You want to retrieve just the person whose ID equals 1, so you specify a Boolean expression, p.ID == 1. Finally, the select clause specifies what Person data you're interested in retrieving.

The ObjectDumper class is a convenient utility for producing formatted output. It has only one method, Write(), which has three overloads. (Both the ObjectDumper.cs source code and the ObjectDumper.dll assembly come with the book's source code download.)

When you run the program you'll see the result in Figure 1-1.

Figure 1-1. Using LINQ to query a list

This very simple example uses new features from C# 3.0. The first is a query expression that is similar to the one used in SQL and allows developers to use query language syntax that they are already accustomed to. When the compiler finds a query expression in the code, it transforms that expression into C# method calls. Listing 1-2 shows how the query expression in Listing 1-1 would be transformed.

Example 1-2. Transformed LINQ to Object Code
var query = people
            .Where(p => p.ID == 1)
            .Select(p => new { p.FirstName, p.LastName } );

The from keyword has been removed, leaving just the collection, people, against which to perform the query. The where and select clauses are transformed into two method calls: Where<T>() and Select<T>(), respectively. They have been concatenated so that the Where method's result is filtered by the Select method.

You may wonder how this is possible. C# 2.0 doesn't provide these methods for the List<T> class or the new C# 3.0 version. C# 3.0 doesn't add these new methods to every class in .NET Framework 3.5. The answer is a new C# 3.0 feature called extension methods.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.26.138