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.

Jo<strong>in</strong>s30111.5.2 Group jo<strong>in</strong>s with jo<strong>in</strong> … <strong>in</strong>to clausesWe’ve seen that the result sequence from a normal jo<strong>in</strong> clause consistsof pairs of elements, one from each of the <strong>in</strong>put sequences. A group jo<strong>in</strong>looks similar <strong>in</strong> terms of the query expression but has a significantly differentoutcome. Each element of a group jo<strong>in</strong> result consists of an elementfrom the left sequence (us<strong>in</strong>g its orig<strong>in</strong>al range variable), and alsoa sequence of all the match<strong>in</strong>g elements of the right sequence, exposed asa new range variable specified by the identifier com<strong>in</strong>g after <strong>in</strong>to <strong>in</strong> thejo<strong>in</strong> clause.Let’s change our previous example to use a group jo<strong>in</strong>. List<strong>in</strong>g 11.13 aga<strong>in</strong> shows allthe defects and the notifications required for each one, but breaks them out <strong>in</strong> a perdefectmanner. Pay particular attention to how we’re display<strong>in</strong>g the results, and to thenested foreach loop.Resultconta<strong>in</strong>sembeddedsubsequencesList<strong>in</strong>g 11.13Jo<strong>in</strong><strong>in</strong>g defects and subscriptions with a group jo<strong>in</strong>var query = from defect <strong>in</strong> SampleData.AllDefectsjo<strong>in</strong> subscription <strong>in</strong> SampleData.AllSubscriptionson defect.Project equals subscription.Project<strong>in</strong>to groupedSubscriptionsselect new { Defect=defect,Subscriptions=groupedSubscriptions };foreach (var entry <strong>in</strong> query){Console.WriteL<strong>in</strong>e(entry.Defect.Summary);foreach (var subscription <strong>in</strong> entry.Subscriptions){Console.WriteL<strong>in</strong>e (" {0}", subscription.EmailAddress);}}The Subscriptions property of each entry is the embedded sequence of subscriptionsmatch<strong>in</strong>g that entry’s defect. Figure 11.7 shows how the two <strong>in</strong>itial sequences arecomb<strong>in</strong>ed.One important difference between an <strong>in</strong>ner jo<strong>in</strong> and a group jo<strong>in</strong>—and <strong>in</strong>deedbetween a group jo<strong>in</strong> and normal group<strong>in</strong>g—is that for a group jo<strong>in</strong> there’s a one-toonecorrespondence between the left sequence and the result sequence, even if someof the elements <strong>in</strong> the left sequence don’t match any elements of the right sequence.This can be very important, and is sometimes used to simulate a left outer jo<strong>in</strong> fromSQL. The embedded sequence is empty when the left element doesn’t match any rightelements. As with an <strong>in</strong>ner jo<strong>in</strong>, a group jo<strong>in</strong> buffers the right sequence but streamsthe left one.List<strong>in</strong>g 11.14 shows an example of this, count<strong>in</strong>g the number of bugs created oneach day <strong>in</strong> August. It uses a DateTimeRange (as described <strong>in</strong> chapter 6) as the leftsequence, and a projection that calls Count on the embedded sequence <strong>in</strong> the resultof the group jo<strong>in</strong>.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!