Bindings with the let Clause

Often, you’ll find it handy to carry out intermediate computations within a query expression, binding the result to an identifier within the query expression. An example is worth a thousand words:

image

Starting from the source selection, we get a sequence of integral numbers 0 through 9 (the parameter 10 indicates the count) from the Range method on Enumerable. Now we’re asked to compute the square of the number, filter out even square values, compute the cube, and so on.

In a typical imperative style, you would write this with intermediate variables containing the results of partial computations. Thanks to the let clause, ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.