03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

LINQ Extension Methods ❘ 187<br />

}<br />

};<br />

CustBalance = customer.Balance<br />

This code defines the methods that are passed as parameters to the query’s Where, OrderBy, and<br />

Select calls, but where are Where, OrderBy, and Select defined They are called as if they are<br />

methods provided by customers, but customers is simply an array of Customer objects and it<br />

doesn’t define those methods.<br />

It turns out that Where, OrderBy, and Select are extension methods added to the IEnumerable<br />

interface by the LINQ library. Arrays implement that interface so they gain these extension methods.<br />

Similarly, LINQ adds other extension methods to the IEnumerable interface such as Any, All,<br />

Average, Count, Distinct, First, GroupBy, OfType, Repeat, Sum, Union, and many more.<br />

Method-Based Queries with Lambda Functions<br />

Lambda expressions make building method-based queries somewhat easier. When you use lambda<br />

expressions, you don’t need to define separate methods to pass as parameters to LINQ methods such<br />

as Where, OrderBy, and Select. Instead, you can pass a lambda expression directly into the method.<br />

The following code shows a revised version of the previous method-based query. Here the method<br />

bodies have been included as lambda expressions.<br />

var query =<br />

customers.Where(customer => customer.Balance < 0).<br />

OrderBy(customer => customer.Balance).<br />

Select(customer => new CustInfo()<br />

{<br />

CustName = customer.FirstName + " " + customer.LastName,<br />

CustBalance = customer.Balance<br />

}<br />

);<br />

This is more concise because it doesn’t require you to build separate methods, but it can be a<br />

lot harder to read and understand. Passing a simple lambda expression to the Where or OrderBy<br />

method may not be too confusing, but if you need to perform complex tests, you may be better<br />

off making separate methods.<br />

Whether you use methods or lambda expressions, the standard LINQ query syntax is usually<br />

easier to understand, so you may prefer to use that version whenever possible. Unfortunately,<br />

many references describe the LINQ extension methods as if you are going to use them in methodbased<br />

queries rather than in LINQ queries. For example, the description of the OrderBy method<br />

at msdn.microsoft.com/library/bb534966.aspx includes the following definition:<br />

public static IOrderedEnumerable OrderBy(<br />

this IEnumerable source,<br />

Func keySelector<br />

)<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!