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.

298 CHAPTER 11 Query expressions and LINQ to Objectsto a sequence of cities by say<strong>in</strong>g that the birth date of the person is the same as thepopulation of the city—it doesn’t make any sense.The syntax for an <strong>in</strong>ner jo<strong>in</strong> looks more complicated than it is:[query select<strong>in</strong>g the left sequence]jo<strong>in</strong> right-range-variable <strong>in</strong> right-sequenceon left-key-selector equals right-key-selectorSee<strong>in</strong>g equals as a contextual keyword rather than us<strong>in</strong>g symbols can be slightly disconcert<strong>in</strong>g,but it makes it easier to dist<strong>in</strong>guish the left key selector from the right keyselector. Often (but not always) at least one of the key selectors is a trivial one that justselects the exact element from that sequence.Let’s look at an example from our defect system. Suppose we had just added thenotification feature, and wanted to send the first batch of emails for all the exist<strong>in</strong>gdefects. We need to jo<strong>in</strong> the list of notifications aga<strong>in</strong>st the list of defects, where theirprojects match. List<strong>in</strong>g 11.12 performs just such a jo<strong>in</strong>.List<strong>in</strong>g 11.12Jo<strong>in</strong><strong>in</strong>g the defects and notification subscriptions based on projectvar query = from defect <strong>in</strong> SampleData.AllDefectsjo<strong>in</strong> subscription <strong>in</strong> SampleData.AllSubscriptionson defect.Project equals subscription.Projectselect new { defect.Summary, subscription.EmailAddress };foreach (var entry <strong>in</strong> query){Console.WriteL<strong>in</strong>e("{0}: {1}", entry.EmailAddress, entry.Summary);}List<strong>in</strong>g 11.12 will show each of the media player bugs twice—once for “mediabugs@skeetysoft.com”and once for “theboss@skeetysoft.com” (because the boss reallycares about the media player project).In this particular case we could easily have made the jo<strong>in</strong> the other way round,revers<strong>in</strong>g the left and right sequences. The result would have been the same entriesbut <strong>in</strong> a different order. The implementation <strong>in</strong> LINQ to Objects returns entries sothat all the pairs us<strong>in</strong>g the first element of the left sequence are returned (<strong>in</strong> theorder of the right sequence), then all the pairs us<strong>in</strong>g the second element of the leftsequence, and so on. The right sequence is buffered, but the left sequence isstreamed—so if you want to jo<strong>in</strong> a massive sequence to a t<strong>in</strong>y one, it’s worth us<strong>in</strong>g thet<strong>in</strong>y one as the right sequence if you can.One error that might trip you up is putt<strong>in</strong>g the key selectors the wrong way round.In the left key selector, only the left sequence range variable is <strong>in</strong> scope; <strong>in</strong> the rightkey selector only the right range variable is <strong>in</strong> scope. If you reverse the left and rightsequences, you have to reverse the left and right key selectors too. Fortunately thecompiler knows that it’s a common mistake and suggests the appropriate course ofaction.Just to make it more obvious what’s go<strong>in</strong>g on, figure 11.6 shows the sequences asthey’re processed.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!