Lambda Expression Query...

  • Thread starter Thread starter Raj
  • Start date Start date
R

Raj

Please observe the code snippet:

Func<String, int, bool> predicate = (str, index) => str.Length == index;

String[] words = { "orange", "apple", "Elephant", "Ant", "star",
"and" };
IEnumerable<String> aWords = words.Where(predicate).Select(str
=> str);

foreach (String word in aWords)
Console.WriteLine(word);

Courtesy: MSDN

How and where value for parameter index of predicate passed?

Thank you

Regards
Raj
 
Raj said:
Please observe the code snippet:

Func<String, int, bool> predicate = (str, index) => str.Length == index;

String[] words = { "orange", "apple", "Elephant", "Ant", "star",
"and" };
IEnumerable<String> aWords = words.Where(predicate).Select(str
=> str);

foreach (String word in aWords)
Console.WriteLine(word);

Courtesy: MSDN

How and where value for parameter index of predicate passed?

Thank you

Regards
Raj

Using the predicate is just as if you put the lambda expression in the
method call:

IEnumerable<String> aWords =
words.Where((str, index) => str.Length == index);

So, it's the Where method that sends the index to the predicate.

(Using .Select(str => str) is pointless, it returns a collection that is
identical to the one you use it on.)
 
Back
Top