I have a string like string strn = "abcdefghjiklmnopqrstuvwxyz"
and want a dictionary like:
Dictionary<char,int>(){
{'a',0},
{'b',1},
{'c',2},
...
}
I've been trying things like
strn.ToDictionary((x,i) => x,(x,i)=>i);
...but I've been getting all sorts of errors about the delegate not taking two arguments, and unspecified arguments, and the like.
What am I doing wrong?
I would prefer hints over the answer so I have a mental trace of what I need to do for next time, but as per the nature of Stackoverflow, an answer is fine as well.
You're assuming there is such an overload. Look at
Enumerable.ToDictionary
- there's no overload which provides the index. You can fake it though via a call toSelect
:You could try something like this:
Use the
.Select
operator first: