Linq: How Indexing Operator works ( Lamda Expression)
-
Tuesday, September 18, 2012 5:47 AM
I have just gone through some Sample LINQ expressions in Web. An Indexed Program is pretty puzzling. Hope many of you know about it. Please find the code attached below.
public void Linq5() { string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var shortDigits = digits.Where((digit, index) => digit.Length < index); Console.WriteLine("Short digits:"); foreach (var d in shortDigits) { Console.WriteLine("The word {0} is shorter than its value.", d); } }Here the operator => is looking to behave very strange. I am new to this LINQ. oreover, I heard about this is a Lamda expression. While I search about Lamda Expression, I could not understand the behavior. Can anyone explain briefly
var shortDigits = digits.Where((digit, index) => digit.Length < index);
tells? I thought that digit and index are all keywords. but it is not.
Kindly explain me.
Viswa V
- Moved by Cindy Meister MVPMVP Tuesday, September 18, 2012 7:22 AM not vsto or Office related (From:Visual Studio Tools for Office)
All Replies
-
Tuesday, September 18, 2012 3:14 PM
Hi Viswa;
The statement:
var shortDigits = digits.Where((digit, index) => digit.Length < index);
This is a Linq to Object query because it queries objects in your program unlike Linq to SQL which queries a database server. The Where in the statement is a extension method which takes as its parameter a lambda expression. You can identify it as a lambda expression because it uses the => operator. A lambda expression is a function that has no name, an anonymous function, and can be thought of as the following.
(input parameters) => ( a statement or statement block )
In a Linq Where extension method the input parameters can be an element of the sequence you are querying or if you have a second parameter it is the zero based index of the sequence. In a lambda expression if there is more then one parameter it must be surrounded by ( ). So in the following lambda expression :
(digit, index) => digit.Length < index
digit is a single instance if the digits array and the second parameter will be the index of that instance of that array. Because this lambda is in a Where extension method the results of the lambda must evaluate to a boolean and so it compares the number of characters in the word with the index in the array and returns true if the length of the word is less then the index of where it is stored in the array.
Fernando (MCSD)
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Marked As Answer by Viswa V Thursday, September 20, 2012 4:18 AM
-
Thursday, September 20, 2012 4:17 AM
Hi Fernando,
Thank you so much for your clear explanation. I got it.
Viswa V
-
Thursday, September 20, 2012 4:25 AM
Not a problem, glad I was able to help.
Fernando (MCSD)
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

