C # Convert list <string> to dictionary <string, string>

This may seem like a strange thing, but, not paying attention to it, there is a good concise way to convert the list into a dictionary, where each pair of key values ​​in the dictionary is just every line in the list. i.e.

List = string1, string2, string3 Dictionary = string1/string1, string2/string2, string3/string3 

I did a lot of searching, and there are literally dozens of examples only on Stackoverflow doing this in the opposite direction, but not like that.

The reason for this is that I have two components of the third part, and changing them out of my hands. One returns a list of email addresses as a list, and the other sends emails, where the To parameter is a dictionary. The dictionary key is the email address, and the value is its real name. However, I don't know the real name, but it still works if you also provide the real name to the email address. So I want to convert the list to a dictionary. There are many ways to do this. The foreach loop in the list that adds kvp to the dictionary. But I love complex code and wonder if there is a single-line solution.

+50
dictionary list c #
Jul 20 2018-12-12T00:
source share
5 answers

Try the following:

 var res = list.ToDictionary(x => x, x => x); 

The first lambda allows you to select a key, the second selects a value.

You can play with it and distinguish values ​​from keys, for example:

 var res = list.ToDictionary(x => x, x => string.Format("Val: {0}", x)); 

If your list contains duplicates, add Distinct() as follows:

 var res = list.Distinct().ToDictionary(x => x, x => x); 

EDIT To comment on the real reason, I think the only reason that can be valid for such conversions is that at some point the keys and values ​​in the resulting dictionary will diverge. For example, you would do the initial conversion, and then replace some of the values ​​with something else. If the keys and values ​​are always the same, the HashSet<String> will provide a much better fit for your situation:

 var res = new HashSet<string>(list); if (res.Contains("string1")) ... 
+113
Jul 20 2018-12-12T00:
source share

Use this:

 var dict = list.ToDictionary(x => x); 

See MSDN for more details.

As Pranyai points out in the comments, this will fail if the item exists in the list more than once.
Depending on your specific requirements, you can use var dict = list.Distinct().ToDictionary(x => x); To get a dictionary of individual elements or use ToLookup :

 var dict = list.ToLookup(x => x); 

This will return ILookup<string, string> , which essentially matches IDictionary<string, IEnumerable<string>> , so you will have a list of individual keys with each instance of the string below it.

+6
Jul 20 2018-12-12T00:
source share

Using ToDictionary :

 var dictionary = list.ToDictionary(s => s); 

If it is possible that any row can be repeated, either first call Distinct on the list (to remove duplicates), or use ToLookup , which allows you to use multiple values ​​for each key.

+1
Jul 20 2018-12-12T00:
source share

EDIT

Another way to handle duplicate is you can do it

 var dic = slist.Select((element, index)=> new{element,index} ) .ToDictionary(ele=>ele.index.ToString(), ele=>ele.element); 

or




easy way to do it

 var res = list.ToDictionary(str => str, str=> str); 

but make sure the line doesn't repeat ... again, otherwise the code above will not work for you

if the line is repeated, what better way to do it

 Dictionary<string,string> dic= new Dictionary<string,string> (); foreach(string s in Stringlist) { if(!dic.ContainsKey(s)) { // dic.Add( value to dictionary } } 
0
Jul 20 2018-12-12T00:
source share

You can use:

 var dictionary = myList.ToDictionary(x => x); 
-one
Jul 20 2018-12-12T00:
source share



All Articles