title: C# - 2 DataTable columns to Dictionary - Distinct Value
body: <p>I have a list of text files, I am reading each and with help of split option I am creating a datatable (Name: tbl) with 5 columns (No ID).</p>
  
  <p>Further, creating a datatable with only Column1 and Column5 and converting this into a dictionary as column1 will be a key and column5 will be its value.</p>
  
  <p>Following is the code:</p>
  
  <pre><code>        string[] selectedColumns = new[] { "Column1", "Column5" };
          DataTable dt = new DataView(tbl).ToTable(false, selectedColumns);
  
          dict = dt.AsEnumerable().ToDictionary&lt;DataRow, string, string&gt;(row =&gt; row[0].ToString(),
                                     row =&gt; row[1].ToString());
  </code></pre>
  
  <p><strong>Error:</strong>
  $exception  {"An item with the same key has already been added."}   System.Exception {System.ArgumentException}</p>
  
  <p>The error arised due to duplication. </p>
  
  <pre><code>    dict = dt.AsEnumerable().Distinct().ToDictionary&lt;DataRow, string, string&gt;(row =&gt; row[0].ToString(),
                                 row =&gt; row[1].ToString());
  </code></pre>
  
  <p>Results into the same.</p>
  
  <pre><code>dict = dt.AsEnumerable().ToDictionary&lt;DataRow, string, string&gt;(row =&gt; row[0].ToString(),
                                             row =&gt; row[1].ToString()).Distinct();
  </code></pre>
  
  <p><strong>Throws an error:</strong> Error  1   Cannot implicitly convert type 'System.Collections.Generic.IEnumerable>' to 'System.Collections.Generic.Dictionary'. An explicit conversion exists (are you missing a cast?)  </p>
  
  <p>How can I avoid duplication and get only distinct values into the dictionary dict?</p>
  
  <p>Thanks In Advance.</p>