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.

Filter<strong>in</strong>g and order<strong>in</strong>g a sequence291The compiler translates this <strong>in</strong>to a call to the Where method with a lambda expression,which uses the appropriate range variable as the parameter and the filterexpression as the body. The filter expression is applied as a predicate to each elementof the <strong>in</strong>com<strong>in</strong>g stream of data, and only those that return true are present <strong>in</strong>the result<strong>in</strong>g sequence. Us<strong>in</strong>g multiple where clauses results <strong>in</strong> multiple cha<strong>in</strong>edWhere calls—only elements that match all of the predicates are part of the result<strong>in</strong>gsequence. List<strong>in</strong>g 11.7 demonstrates a query expression that f<strong>in</strong>ds all open defectsassigned to Tim.List<strong>in</strong>g 11.7Query expression us<strong>in</strong>g multiple where clausesUser tim = SampleData.Users.TesterTim;var query = from bug <strong>in</strong> SampleData.AllDefectswhere bug.Status != Status.Closedwhere bug.AssignedTo == timselect bug.Summary;foreach (var summary <strong>in</strong> query){Console.WriteL<strong>in</strong>e(summary);}The query expression <strong>in</strong> list<strong>in</strong>g 11.7 is translated <strong>in</strong>toSampleData.AllDefects.Where (bug => bug.Status != Status.Closed).Where (bug => bug.AssignedTo == tim).Select(bug => bug.Summary)The output of list<strong>in</strong>g 11.7 is as follows:Installation is slowSubtitles only work <strong>in</strong> WelshPlay button po<strong>in</strong>ts the wrong wayWebcam makes me look baldNetwork is saturated when play<strong>in</strong>g WAV fileOf course, we could write a s<strong>in</strong>gle where clause that comb<strong>in</strong>ed the two conditions asan alternative to us<strong>in</strong>g multiple where clauses. In some cases this may improve performance,but it’s worth bear<strong>in</strong>g the readability of the query expression <strong>in</strong> m<strong>in</strong>d, too.Once more, this is likely to be a fairly subjective matter. My personal <strong>in</strong>cl<strong>in</strong>ation is tocomb<strong>in</strong>e conditions that are logically related but keep others separate. In this case,both parts of the expression deal directly with a defect (as that’s all our sequence conta<strong>in</strong>s),so it would be reasonable to comb<strong>in</strong>e them. As before, it’s worth try<strong>in</strong>g bothforms to see which is clearer.In a moment, we’ll start try<strong>in</strong>g to apply some order<strong>in</strong>g rules to our query, but firstwe should look at a small detail to do with the select clause.11.3.2 Degenerate query expressionsWhile we’ve got a fairly simple translation to work with, let’s revisit a po<strong>in</strong>t I glossedover earlier <strong>in</strong> section 11.2.2 when I first <strong>in</strong>troduced the compiler translations. So far,Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!