title: LINQ operations on IDictionary
body: <p>Let's suppose I have the following dictionaries:</p>
  
  <pre><code>private Dictionary&lt;int, string&gt; dic1 = new Dictionary&lt;int, string&gt;()
  {
        { 1, "a" },
        { 2, "b" },
        { 3, "c" }
  }
  
  private Dictionary&lt;SomeEnum, bool&gt; dic2 = new Dictionary&lt;SomeEnum, bool&gt;()
  {
        { SomeEnum.First, true },
        { SomeEnum.Second, false },
        { SomeEnum.Third, false }
  }
  </code></pre>
  
  <p>I want to convert these two dictionaries into a <code>Dictionary&lt;string, object&gt;</code></p>
  
  <p>For example:</p>
  
  <pre><code> dic1 = new Dictionary&lt;string, object&gt;()
   {
       { "1", "a" },
       { "2", "b" },
       { "3", "c" }
   }
  
   dic2 = new Dictionary&lt;string, object&gt;()
   {
       { "First", true },
       { "Second", false },
       { "Third", false }
   }
  </code></pre>
  
  <p>As you can see, the <code>string</code> key of these dictionaries is just the <code>string</code> representation of the previous ones.</p>
  
  <p>The method that is responsible for the conversion has the following signature:</p>
  
  <pre><code> public static object MapToValidType(Type type, object value)
   {
       //....
  
       if(typeof(IDictionary).IsAssignableFrom(type))
       {
           //I have to return a Dictionary&lt;string, object&gt; here
           return ??;
       }
   }
  </code></pre>
  
  <p>I have tried the following:</p>
  
  <pre><code> ((IDictionary)value).Cast&lt;object&gt;().ToDictionary(i =&gt; ...);
  </code></pre>
  
  <p>But <code>i</code> was casted to an object, so I cannot access the key or value items. For that I would need to cast it to the appropiate <code>KeyValuePair&lt;TKey, TValue&gt;</code>, but I don't know <code>TKey</code> or <code>TValue</code> type.</p>
  
  <p>Another solution is to do this:</p>
  
  <pre><code>IDictionary dic = (IDictionary)value;
  IList&lt;string&gt; keys = dic.Keys.Cast&lt;object&gt;().Select(k =&gt; Convert.ToString(k)).ToList();
  IList&lt;object&gt; values = dic.Values.Cast&lt;object&gt;().ToList();
  Dictionary&lt;string, object&gt; newDic = new Dictionary&lt;string, object&gt;();
  for(int i = 0; i &lt; keys.Count; i++)
     newDic.Add(keys[0], values[0]);
  return newDic;
  </code></pre>
  
  <p>However, I'm not much of a fan of this approach and I am really looking for a simpler and friendlier one line LINQ statement.</p>