Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert List of KeyValuePair into IDictionary "C#"

Tags:

c#

idictionary

My scenario,

how to convert List<KeyValuePair<string, string>> into IDictionary<string, string>?

like image 539
anbuselvan Avatar asked Oct 26 '10 09:10

anbuselvan


People also ask

How to convert List of KeyValuePair to Dictionary?

IDictionary<string, string> dictionary = list. ToDictionary(pair => pair. Key, pair => pair. Value);

What is the difference between KeyValuePair and dictionary C#?

KeyValuePair is the unit of data stored in a Hashtable (or Dictionary ). They are not equivalent to each other. A key value pair contains a single key and a single value. A dictionary or hashtable contains a mapping of many keys to their associated values.

How do you create a key value pair?

To add key-value pair in C# Dictionary, firstly declare a Dictionary. IDictionary<int, string> d = new Dictionary<int, string>(); Now, add elements with KeyValuePair. d.

How do I create a dictionary with a KeyValuePair?

To start, we include the System.Collections.Generic and System.Linq namespaces. We create and populate a Dictionary collection. Next, we call ToList () on that Dictionary collection, yielding a List of KeyValuePair instances. Then: We loop over the List instance, using a foreach-loop with the KeyValuePair iteration variable.

Is it possible to convert list<keyvaluepairs<T1> to dictionary<T2>?

It's easy to create a List<KeyValuePairs<T1, T2>>, and conceptually, it's the same thing as a Dictionary, but I just couldn't find a way to convert. The ToDictionary () extension method allows you to specify which parts of your list item you want for a key and value, including the key/value of KeyValuePair<> list item.

How do I create a list of KeyValuePair instances?

ToList is part of the System.Linq extensions to the C# language. Example. To start, we include the System.Collections.Generic and System.Linq namespaces. We create and populate a Dictionary collection. Next, we call ToList () on that Dictionary collection, yielding a List of KeyValuePair instances.

How to convert a dictionary to a list of pairs?

Convert dictionary, list. A Dictionary can be converted into a List. Specifically it is converted to a List of pairs. This requires the ToList extension method. ToList is part of the System.Linq extensions to the C# language. Example. To start, we include the System.Collections.Generic and System.Linq namespaces.


2 Answers

Very, very simply with LINQ:

IDictionary<string, string> dictionary =     list.ToDictionary(pair => pair.Key, pair => pair.Value); 

Note that this will fail if there are any duplicate keys - I assume that's okay?

like image 118
Jon Skeet Avatar answered Sep 19 '22 13:09

Jon Skeet


Or you can use this extension method to simplify your code:

public static class Extensions {     public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(         this IEnumerable<KeyValuePair<TKey, TValue>> list)     {             return list.ToDictionary(x => x.Key, x => x.Value);     }  } 
like image 28
Кеша Шушпанов Avatar answered Sep 19 '22 13:09

Кеша Шушпанов