FREE BOOK

Chapter 11: An introduction to LINQ

Posted by Murach Free Book | LINQ August 13, 2009
In this chapter, you’ll learn the basic concepts and skills for using a new feature of C# 2008 called LINQ. To illustrate these concepts and skills, I’ll use an implementation of LINQ called LINQ to Objects. You use LINQ to Objects to work with in-memory data structures such as generic lists and arrays.

How to use extension methods and lambda expressions

Earlier in this chapter, I mentioned that the query operators provided by LINQ are implemented as methods and that you can call these methods directly rather than use the C# clauses for LINQ. In the topics that follow, you'll learn how these methods work and how you use them to implement LINQ functionality.

How extension methods work

Most of the methods that provide for LINQ functionality are implemented as extension methods. An extension method is similar to a regular method except that it's defined outside the data type that it's used with. The example in figure 11-11 shows how this works. Here, a method named FormattedPhoneNumber is implemented as an extension method of the String class.

You should notice three things about the code that implements this method. First, it's a static method that's stored in a static class, which is a requirement for extension methods. Second, the data type of the first parameter of the method identifies the .NET class or structure that the method extends. In this case, the parameter is a string, so the method extends the String class. Third, the declaration for the first parameter is preceded by the this keyword.

Once you've defined an extension method, you can use it as shown in this figure. To start, you declare a variable with the same type as the first parameter of the method. In this case, a string variable is declared and assigned the digits of a phone number. Then, the extension method is executed as a method of that string. Notice that you don't pass a value to the first parameter of the method. Instead, the value of the object on which the method is executed is assigned to this parameter. If any additional parameters are defined by the method, though, their values are passed to the method as shown here.

Extension methods used to implement LINQ functionality

Now that you have an idea of how extension methods work, you may want to know what methods are used to implement some of the common C# clauses for LINQ. These methods are listed in the table in figure 11-11. In the next figure, you'll see how you can use some of these methods in a query. Then, you'll learn about the extension methods that implement aggregate functions, which aren't available as C# clauses.

Extension methods used to implement common C# clauses for LINQ

Clause Method
where Where
orderby OrderBy, OrderByDescending, ThenBy, ThenByDescending
select Select
join Join
group GroupBy

An extension method that extends the String data type

A class with an extension method that formats a phone number

public static class StringExtensions

{

   public static string FormattedPhoneNumber(this string phone,

   string separator)

   {

       return phone.Substring(0, 3) + separator

       + phone.Substring(3, 3) + separator

       + phone.Substring(6, 4);

   }

}

Code that uses the extension method

string phoneNumber = "5595551212";
string formattedPhoneNumber = phoneNumber.FormattedPhoneNumber(".");
MessageBox.Show(formattedPhoneNumber, "Extension Method");

The resulting dialog box

Description

  • C# uses extension methods to implement the standard query operators provided by LINQ. These methods are defined for the Enumerable and Queryable classes.
  • Extension methods provide for adding methods to a data type from outside the definition of that data type. Extension methods must be coded within a static class.
  • The first parameter of an extension method identifies the data type it extends and must be preceded by the this keyword. You don't pass a value to this parameter when you call the method. Instead, you call the method on an instance of the data type identified by the first parameter.

Figure 11-11 How to use extension methods

How lambda expressions work

When you code a query using extension methods, you need to know how to code lambda expressions. In short, a lambda expression is a function without a name that evaluates an expression and returns its value. Figure 11-12 presents the syntax of a lambda expression, which consists of a parameter list followed by the lambda operator (=>, read as “goes to”) and an expression. Note that if the lambda expression uses more than one parameter, the parameter list must be enclosed in parentheses. Otherwise, the parentheses can be omitted.

To use a lambda expression, you assign it to a delegate type, which specifies the signature of a method. The first example in this figure illustrates how this works. Here, the first statement defines a delegate type named compareDel that accepts a decimal value and returns a Boolean value. Then, the second statement declares a delegate of that type named invoiceOver20000 and assigns a lambda expression to it. In this case, the lambda expression checks if a decimal parameter named total is greater than 20,000. If so, the expression will return a true value. Otherwise, it will return a false value.

In this example, the lambda expression is assigned to a variable. Then, in the code that executes the lambda expression, that variable is used to refer to the lambda expression and pass in the decimal value. However, you can also code a lambda expression in-line. You'll see how that works next.

Total Pages : 10 678910

comments