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.
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: Let,Linq-To-Sql,Linq