I've had a look at this question and while it solves their issue at hand, its not jumping out at me and I am still having trouble with mine.
I have the following code, where a string is being split into its prospective key/value pairs however it would normally dump this out into a <string, string>
dictionary. I need the value to be in the form of a enum as follows:
public enum SortDirection
{
asc = 0,
desc
}
I have declared the dictionary orderBy as follows:
var orderedBy = new Dictionary<string, SortDirection>();
keyValuePair.Value is a set of key value pairs in a string one after the other.
ClientNo asc,ClientLastName asc
and the code to split this and create the dictionary is as follows.
orderedBy = keyValuePair.Value.Split(',')
.Select(x => x.Split(' '))
.ToDictionary(x => x[0], (x => Enum.TryParse(x[1], false, out SortDirection direction)));
At no time will the value for x[1]
be anything other than either "asc" or "desc" however I cannot seem to convert the string value to an enum on the fly.
I am getting the following error.
Cannot convert type 'System.Collections.Generic.KeyValuePair' to 'System.Collections.Generic.KeyValuePair'
How do I convert x[1]
from a string ("asc", "desc") to an enum in the code above and thus populate the dictionary "orderBy" via the Linq .ToDictionary
?