13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Jo<strong>in</strong>s29711.5 Jo<strong>in</strong>sEach part of the query has been adjusted appropriately: where the orig<strong>in</strong>al queryexpression referenced user or length directly, if the reference occurs after the letclause, it’s replaced by z.user or z.length. The choice of z as the name here is arbitrary—it’sall hidden by the compiler.If you read the <strong>C#</strong> 3 language specification on let clauses, you’ll see that the translationit describes is from one query expression to another. It uses an asterisk (*) torepresent the transparent identifier <strong>in</strong>troduced. The transparent identifier is thenerased as a f<strong>in</strong>al step <strong>in</strong> translation. I won’t use that notation <strong>in</strong> this chapter, as it’s hardto come to grips with and unnecessary at the level of detail we’re go<strong>in</strong>g <strong>in</strong>to. Hopefullywith this background, the specification won’t be quite as impenetrable as it mightbe otherwise, should you need to refer to it.The good news is that we can now take a look at the rest of the translations mak<strong>in</strong>gup <strong>C#</strong> 3’s query expression support. I won’t go <strong>in</strong>to the details of every transparentidentifier <strong>in</strong>troduced, but I’ll mention the situations <strong>in</strong> which they occur. Let’s look atthe support for jo<strong>in</strong>s first.If you’ve ever read anyth<strong>in</strong>g about SQL, you probably have an idea what a database jo<strong>in</strong>is. It takes two tables and creates a result by match<strong>in</strong>g one set of rows aga<strong>in</strong>st anotherset of rows. A LINQ jo<strong>in</strong> is similar, except it works on sequences. Three types of jo<strong>in</strong>sare available, although not all of them use the jo<strong>in</strong> keyword <strong>in</strong> the query expression.We’ll start with the jo<strong>in</strong> that is closest to a SQL <strong>in</strong>ner jo<strong>in</strong>.11.5.1 Inner jo<strong>in</strong>s us<strong>in</strong>g jo<strong>in</strong> clausesInner jo<strong>in</strong>s <strong>in</strong>volve two sequences. One key selector expression is applied to each elementof the first sequence and another key selector (which may be totally different) isapplied to each element of the second sequence. The result of the jo<strong>in</strong> is a sequenceof all the pairs of elements where the key from the first element is the same as the keyfrom the second element.NOTETerm<strong>in</strong>ology clash! Inner and outer sequences—The MSDN documentation forthe Jo<strong>in</strong> method used to evaluate <strong>in</strong>ner jo<strong>in</strong>s unhelpfully calls thesequences <strong>in</strong>volved <strong>in</strong>ner and outer. This has noth<strong>in</strong>g to do with <strong>in</strong>nerjo<strong>in</strong>s and outer jo<strong>in</strong>s—it’s just a way of differentiat<strong>in</strong>g between thesequences. You can th<strong>in</strong>k of them as first and second, left and right, Bertand Ernie—anyth<strong>in</strong>g you like that helps you. I’ll use left and right for thischapter. Aside from anyth<strong>in</strong>g else, it makes it obvious which sequence iswhich <strong>in</strong> diagram form.The two sequences can be anyth<strong>in</strong>g you like: the right sequence can even be the sameas the left sequence, if that’s useful. (Imag<strong>in</strong>e f<strong>in</strong>d<strong>in</strong>g pairs of people who were bornon the same day, for example.) The only th<strong>in</strong>g that matters is that the two key selectorexpressions must result <strong>in</strong> the same type of key 4 . You can’t jo<strong>in</strong> a sequence of people4It is also valid for there to be two key types <strong>in</strong>volved, with an implicit conversion from one to the other. Oneof the types must be a better choice than the other, <strong>in</strong> the same way that the compiler <strong>in</strong>fers the type of animplicitly typed array. In my experience you rarely need to consciously consider this detail.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!