Lambda Expression Query...

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
 
G

Göran Andersson

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top