I have a List
and I want to convert it to a Dictionary
. I don't have any problem in converting a List
to Dictionary
but I want to check if the key in the Dictionary
is present or not, if present I should neglect the KeyValuePair
and proceed with the next string.
Here is the example.
IEnumerable<String> readlines = File.ReadAllLines("C:\Text.txt");
List<string> lines = new List<string>();
lines = readlines.ToList();
Dictionary<string, string> keyPair= new Dictionary<string, string>();
keyPair= test.ToDictionary(s=>s.Split('=')[1], s=>s.Split('=')[0]);
keyPair.Keys.ToList().Sort();
If the same key repeats , the lambda expression throws and exception and I want to check whether the key exists or not and proceed with converting next string to dictionary. I want to convert the dictionary back to List once the key sorting is done. Is there any way to do it with LINQ?