13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

284 CHAPTER 11 Query expressions and LINQ to ObjectsList<strong>in</strong>g 11.1Trivial query to pr<strong>in</strong>t the list of usersvar query = from user <strong>in</strong> SampleData.AllUsersselect user;foreach (var user <strong>in</strong> query){Console.WriteL<strong>in</strong>e(user);}The query expression is the part highlighted <strong>in</strong> bold. I’ve overridden ToStr<strong>in</strong>g foreach of the entities <strong>in</strong> the model, so the results of list<strong>in</strong>g 11.1 are as follows:User: Tim Trotter (Tester)User: Tara Tutu (Tester)User: Deborah Denton (Developer)User: Darren Dahlia (Developer)User: Mary Malcop (Manager)User: Col<strong>in</strong> Carton (Customer)You may be wonder<strong>in</strong>g how useful this is as an example: after all, we could have justused SampleData.AllUsers directly <strong>in</strong> our foreach statement. However, we’ll use thisquery expression—however trivial—to <strong>in</strong>troduce two new concepts. First we’ll look atthe general nature of the translation process the compiler uses when it encounters aquery expression, and then we’ll discuss range variables.11.2.2 Compiler translations as the basis of query expressionsThe <strong>C#</strong> 3 query expression support is based on the compiler translat<strong>in</strong>g query expressions<strong>in</strong>to “normal” <strong>C#</strong> code. It does this <strong>in</strong> a mechanical manner that doesn’t try tounderstand the code, apply type <strong>in</strong>ference, check the validity of method calls, or anyof the normal bus<strong>in</strong>ess of a compiler. That’s all done later, after the translation. Inmany ways, this first phase can be regarded as a preprocessor step. The compiler translateslist<strong>in</strong>g 11.1 <strong>in</strong>to list<strong>in</strong>g 11.2. before do<strong>in</strong>g the real compilation.List<strong>in</strong>g 11.2The query expression of list<strong>in</strong>g 11.1 translated <strong>in</strong>to a method callvar query = SampleData.AllUsers.Select(user => user);foreach (var user <strong>in</strong> query){Console.WriteL<strong>in</strong>e(user);}The <strong>C#</strong> 3 compiler translates the query expression <strong>in</strong>to exactly that code before properlycompil<strong>in</strong>g it further. In particular, it doesn’t assume that it should use Enumerable.Select, or that List will conta<strong>in</strong> a method called Select. It merely translates thecode and then lets the next phase of compilation deal with f<strong>in</strong>d<strong>in</strong>g an appropriatemethod—whether as a straightforward member or as an extension method. The parametercan be a suitable delegate type or an Expression for an appropriate type T.This is where it’s important that lambda expressions can be converted <strong>in</strong>toboth delegate <strong>in</strong>stances and expression trees. All the examples <strong>in</strong> this chapter willLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!