Tuesday, March 9, 2010

LINQ let Keyword

How can I miss that! :)

I have been working with LINQ for almost 2 years, and I've never noticed the new keyword "let" which is used inside LINQ queries to create temporarily variables.

var list = new List() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var query = from i in list
let j = i + 2
let k = j * j
select new { i, j, k };

You can see the let keyword was used to create two new temp variables (j, k) which their values were calculated inside the query.

Looks nice, doesn't it!!

No comments: