O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

var res = from i in Enumerable.Range(0, 10)          let square = i * i          where square % 2 == 0          let cube = i * square          let stars = new string('*', i)          select new { Stars = stars, Cube = cube };

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required