title: Map two lists into a dictionary in C#
body: <p><strong>Given two</strong> <code>IEnumerable</code>s <strong>of the same size, how can I convert it to a</strong> <code>Dictionary</code> <strong>using Linq?</strong></p>
  
  <pre><code>IEnumerable&lt;string&gt; keys = new List&lt;string&gt;() { "A", "B", "C" };
  IEnumerable&lt;string&gt; values = new List&lt;string&gt;() { "Val A", "Val B", "Val C" };
  
  var dictionary = /* Linq ? */;
  </code></pre>
  
  <p>And the expected output is:</p>
  
  <pre><code>A: Val A
  B: Val B
  C: Val C
  </code></pre>
  
  <p>I wonder if there is some simple way to achieve it.</p>
  
  <p>And should I be worried about performance? What if I have large collections?</p>
  
  <hr>
  
  <p>I don't if there is an easier way to do it, currently I'm doing like this:</p>
  
  <p>I have an Extension method that will loop the <code>IEnumerable</code> providing me the element and the index number.</p>
  
  <pre><code>public static class Ext
  {
      public static void Each&lt;T&gt;(this IEnumerable els, Action&lt;T, int&gt; a)
      {
          int i = 0;
          foreach (T e in els)
          {
              a(e, i++);
          }
      }
  }
  </code></pre>
  
  <p>And I have a method that will loop one of the Enumerables and with the index retrieve the equivalent element on the other Enumerable.</p>
  
  <pre><code>public static Dictionary&lt;TKey, TValue&gt; Merge&lt;TKey, TValue&gt;(IEnumerable&lt;TKey&gt; keys, IEnumerable&lt;TValue&gt; values)
  {
      var dic = new Dictionary&lt;TKey, TValue&gt;();
  
      keys.Each&lt;TKey&gt;((x, i) =&gt;
      {
          dic.Add(x, values.ElementAt(i));
      });
  
      return dic;
  }
  </code></pre>
  
  <p>Then I use it like:</p>
  
  <pre><code>IEnumerable&lt;string&gt; keys = new List&lt;string&gt;() { "A", "B", "C" };
  IEnumerable&lt;string&gt; values = new List&lt;string&gt;() { "Val A", "Val B", "Val C" };
  
  var dic = Util.Merge(keys, values);
  </code></pre>
  
  <p>And the output is correct:</p>
  
  <pre><code>A: Val A
  B: Val B
  C: Val C
  </code></pre>