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.

366 APPENDIX LINQ standard query operatorssequence. Both use deferred execution, and stream the left sequence but buffer theright sequence.Table A.8Jo<strong>in</strong> examplesExpressionnames.Jo<strong>in</strong> // Left sequence(colors, // Right sequencename => name[0], // Left key selectorcolor => color[0], // Right key selector// Projection for result pairs(name, color) => name+" - "+color)names.GroupJo<strong>in</strong>(colors,name => name[0],color => color[0],// Projection for key/sequence pairs(name, matches) => name+": "+str<strong>in</strong>g.Jo<strong>in</strong>("/", matches.ToArray()))Result"Rob<strong>in</strong> - Red","Ruth - Red","Bob - Blue","Bob - Beige""Rob<strong>in</strong>: Red","Ruth: Red","Bob: Blue/Beige","Emma: "For the jo<strong>in</strong> examples <strong>in</strong> table A.8, we’ll match a sequence of names (Rob<strong>in</strong>, Ruth,Bob, Emma) aga<strong>in</strong>st a sequence of colors (Red, Blue, Beige, Green) by look<strong>in</strong>g at thefirst character of both the name and the color, so Rob<strong>in</strong> will jo<strong>in</strong> with Red and Bobwill jo<strong>in</strong> with both Blue and Beige, for example.Note that Emma doesn’t match any of the colors—the name doesn’t appear at all<strong>in</strong> the results of the first example, but it does appear <strong>in</strong> the second, with an emptysequence of colors.A.9 Partition<strong>in</strong>gThe partition<strong>in</strong>g operators either skip an <strong>in</strong>itial part of the sequence, return<strong>in</strong>g only therest, or take only the <strong>in</strong>itial part of a sequence, ignor<strong>in</strong>g the rest. In each case you caneither specify how many elements are <strong>in</strong> the first part of the sequence, or specify a condition—thefirst part of the sequence cont<strong>in</strong>ues until the condition fails. After the conditionfails for the first time, it isn’t tested aga<strong>in</strong>—it doesn’t matter whether laterelements <strong>in</strong> the sequence match or not. All of the partition<strong>in</strong>g operators (see table A.9)use deferred execution.Table A.9Partition<strong>in</strong>g examplesExpressionResultwords.Take(3)words.Skip(3)"zero", "one", "two""three", "four"Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!