iAteABug_And_iLiked_it
iAteABug_And_iLiked_it

Reputation: 3795

Explanation of how this lambda expression in plain English?

I converted a query syntax Select example from MSDN into Lambda. It works, and I have written it myself but I can't get my head around the commented line below. I mean, I am selecting from numbers array, and yet it works fine and instead of digits shows equivalent strings . How does it match the two arrays?

  int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
  string[] strings = {"zero", "one", "two", "three", "four",
                       "five", "six", "seven", "eight", "nine" }; 

  //Confusing line: **How would we represent this line below in plain english?**
  var result = numbers.Select(d => strings[d]);
  foreach (var d in result)

  {
   Console.WriteLine(d); 
  }

Output:

five 
four
one 
..rest of numbers follow

Original MSDN Code in Query syntax:

var result= 
        from n in numbers 
        select strings[n];       

    foreach (var s in strings) 
    { 
        Console.WriteLine(s); 
    } 

Maybe explaining such a thing is a bit tricky, but I'm hoping someone has just the right words so it makes sense :)

Thank you

Upvotes: 4

Views: 268

Answers (6)

mao47
mao47

Reputation: 967

I think the reason it seems strange is the data used as an example. This line:

var result = numbers.Select(d => strings[d]);

is simply taking the integers in numbers and using them to index strings, and put that in a new IEnumerable result. If you look closely at strings, you'll notice that:

strings[0] = "zero"
strings[1] = "one"
strings[2] = "two"
... and so on

So the strings array is really acting as a dictionary, allowing you to look up the string representation of a number by providing it as an int. That means when you index strings[d] in your query, you are taking the integer from numbers and translating that value into a string.

Upvotes: 3

Khan
Khan

Reputation: 18142

Look at .Select() as "create an IEnumerable of whatever comes after the =>."

As a foreach, it would look like:

var result = new List<string>();
foreach(var d in numbers)
{
    result.Add(strings[d]);
}
return result;

Upvotes: 5

Habib
Habib

Reputation: 223187

I am selecting from numbers array, and yet it works fine and instead of digits shows equivalent strings.

Its your Select clause which is selecting from your strings array at the index of d, where d is each number in the int array numbers

Upvotes: 3

King King
King King

Reputation: 63317

The two arrays have the same Length and the LINQ in fact does some loop through the numbers, passes in the index into the strings and returns the strings[index].

The point is the numbers mustn't contain any element which is equal or greater than the Length of the strings

Upvotes: 1

keyboardP
keyboardP

Reputation: 69362

I'd say something along the lines of

Go through each integer in the numbers array, select the element at that integer position within the strings array, and place it in an IEnumerable collection.

Upvotes: 1

Magnus Grindal Bakken
Magnus Grindal Bakken

Reputation: 2113

A humble attempt:

"For each number in the list called numbers, take the string in the corresponding position from the list of strings called strings."

Upvotes: 3

Related Questions