Reputation: 2135
I have a collection I want to transform into a dictionary. Here's the code:
myCollection.ToDictionary(item => item.Split('=')[0], item => item.Split('=')[1]);
Being the collection something like:
{"a=312d","b=dw234","c=wqdqw3=3")
The problem comes at the third object. As you can see, it has a second equal inside of it. This one, and all the character after it, are also part of the value (in the dictionary it should be c:wqdqw3=3
). But, as you can imagine, I'm getting something like this in my dictionary a:312d, b:dw234, c:wqdqw3
.
How could you change it so that the value of the dictionary was, for each element of the collection, everything that comes after the first '='?
Upvotes: 3
Views: 297
Reputation: 45947
IndexOf()
and Substring()
should help here
string[] input = { "a=312d", "b=dw234", "c=wqdqw3=3" };
var result = input.ToDictionary(x => x.Substring(0, x.IndexOf('=')),
x => x.Substring(x.IndexOf('=') + 1));
Upvotes: 3