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.

304 CHAPTER 11 Query expressions and LINQ to Objectsthe first two from clauses, then cross jo<strong>in</strong> the result<strong>in</strong>g sequence with the next fromclause, and so on. Each extra from clause adds its own range variable.List<strong>in</strong>g 11.15 shows a simple (but useless) cross jo<strong>in</strong> <strong>in</strong> action, produc<strong>in</strong>g asequence where each entry consists of a user and a project. I’ve deliberately pickedtwo completely unrelated <strong>in</strong>itial sequences to show that no match<strong>in</strong>g is performed.List<strong>in</strong>g 11.15Cross jo<strong>in</strong><strong>in</strong>g users aga<strong>in</strong>st projectsvar query = from user <strong>in</strong> SampleData.AllUsersfrom project <strong>in</strong> SampleData.AllProjectsselect new { User=user, Project=project };foreach (var pair <strong>in</strong> query){Console.WriteL<strong>in</strong>e("{0}/{1}",pair.User.Name,pair.Project.Name);}The output of list<strong>in</strong>g 11.15 beg<strong>in</strong>s like this:Tim Trotter/Skeety Media PlayerTim Trotter/Skeety TalkTim Trotter/Skeety OfficeTara Tutu/Skeety Media PlayerTara Tutu/Skeety TalkTara Tutu/Skeety OfficeFigure 11.8 shows the sequences <strong>in</strong>volved to get this result.If you’re familiar with SQL, you’re probably quite comfortable so far—it looks justlike a Cartesian product obta<strong>in</strong>ed from a query specify<strong>in</strong>g multiple tables. Indeed,most of the time that’s exactly how cross jo<strong>in</strong>s are used. However, there’s more poweravailable when you want it: the right sequence used at any particular po<strong>in</strong>t <strong>in</strong> time candepend on the “current” value of the left sequence. When this is the case, it’s not across jo<strong>in</strong> <strong>in</strong> the normal sense of the term. The query expression translation is thesame whether or not we’re us<strong>in</strong>g a true cross jo<strong>in</strong>, so we need to understand the morecomplicated scenario <strong>in</strong> order to understand the translation process.Before we dive <strong>in</strong>to the details, let’s see the effect it produces. List<strong>in</strong>g 11.16 shows asimple example, us<strong>in</strong>g sequences of <strong>in</strong>tegers.List<strong>in</strong>g 11.16Cross jo<strong>in</strong> where the right sequence depends on the left elementvar query = from left <strong>in</strong> Enumerable.Range(1, 4)from right <strong>in</strong> Enumerable.Range(11, left)select new { Left=left, Right=right };foreach (var pair <strong>in</strong> query){Console.WriteL<strong>in</strong>e("Left={0}; Right={1}",pair.Left, pair.Right);}Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!