Tuesday, August 24, 2010

Using Let Keyword in Linq

I am using Linq-To-Object in my current project to remove some extra loops and I have found one of the great keyword in Linq called ‘Let’. Let keyword provides facility to declare a temporary variable inside the Linq Query.We can assign the result of manipulation to temporary variable inside query and we can use that temporary variable to another manipulation.

Let’s take a simple example of Linq query I am using an integer array to find square and after finding the square of the integer value I will use let keyword to find square value which are greater then 20. Here is the my query for that.

protected void Page_Load(object sender, EventArgs e)
{

int[] intArray = { 1, 2, 3, 4, 5, 6, 7, 8 };
var Result = from i in intArray
let square = i * i
where square>20
select square;
foreach (int i in Result)
{
Response.Write(i.ToString());
Response.Write("\n");
}
}
Here is the result of that query as expected.
LetResult

Let keyword is more useful when you are working with directories and files,xml manipulations so here possibilities are unlimited. Hope this will help you.. Happy Programming!!!

Technorati Tags: ,,
Shout it
kick it on DotNetKicks.com
Share:

0 comments:

Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews