0

I am wondering how this statement works while converting a list to a dictionary. The expression is much like

OtherList = MyList.ToDictionary(x => x.MyId, y => y.MyName);

can anybody explain that why there are two variables x and y ? And how they work together ? Cant I use the same variable twice ?

Necromancer
  • 194
  • 3
  • 13
  • They are two separate [lambda expressions](https://msdn.microsoft.com/en-GB/library/bb397687.aspx). – Phylogenesis Oct 20 '15 at 12:05
  • 1
    [Here](http://stackoverflow.com/questions/3970219/c-sharp-lambda) is another duplicate. Note that both variables could have the same name(f.e. `x`) since they are in different scopes. – Tim Schmelter Oct 20 '15 at 12:05
  • If you're used to other LINQ, like `Select`, you see pretty much this same code. The difference with `ToDictionary` is that, to obtain the information it needs, it simply requires two methods rather than one. – Jonesopolis Oct 20 '15 at 12:06
  • 1
    The first lambda is selecting the key to use in the dictionary from each item in your list. The second selects the value. The variable names `x` and `y` are arbitrary and both represent one item in the list. – juharr Oct 20 '15 at 13:05

0 Answers0