title: C# Linq .ToDictionary() Key Already Exists
body: <p><b>Final Edit: I was able to locate the duplicate field in the ini file. Thanks for your help everyone!</b></p>
  
  <p>I'm using a regular expression to parse an ini file and LINQ to store it in a Dictionary:</p>
  
  <blockquote>
    <p><b>Sample Data:</b><br>
    [WindowSettings]<br>
    Window X Pos='0'<br>
    Window Y Pos='0'<br>
    Window Maximized='false'<br>
    Window Name='Jabberwocky'<br>
    <br>
    [Logging]<br>
    Directory='C:\Rosetta Stone\Logs'<br></p>
  </blockquote>
  
  <p><b>EDIT: Here is the file actually causing the problem: <a href="http://pastebin.com/mQSrkrcP" rel="noreferrer">http://pastebin.com/mQSrkrcP</a> </b></p>
  
  <p><b>EDIT2: I've narrowed it down to being caused by the last section in the file: [list_first_nonprintable]</b></p>
  
  <p>For some reason one of the files that I'm parsing with this is throwing this exception:</p>
  
  <blockquote>
    <p>System.ArgumentException: An item with the same key has already been added.</p>
  </blockquote>
  
  <p>Is there any way for me to either find out which key is causing the problem (so I can fix the file), or to just skip the key that's causing this and continue parsing?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>try
  {
      // Read content of ini file.
      string data = System.IO.File.ReadAllText(project);
  
      // Create regular expression to parse ini file.
      string pattern = @"^((?:\[)(?&lt;Section&gt;[^\]]*)(?:\])(?:[\r\n]{0,}|\Z))((?!\[)(?&lt;Key&gt;[^=]*?)(?:=)(?&lt;Value&gt;[^\r\n]*)(?:[\r\n]{0,4}))*";
      //pattern = @"
      //^                           # Beginning of the line
      //((?:\[)                     # Section Start
      //     (?&lt;Section&gt;[^\]]*)     # Actual Section text into Section Group
      // (?:\])                     # Section End then EOL/EOB
      // (?:[\r\n]{0,}|\Z))         # Match but don't capture the CRLF or EOB
      // (                          # Begin capture groups (Key Value Pairs)
      //  (?!\[)                    # Stop capture groups if a [ is found; new section
      //  (?&lt;Key&gt;[^=]*?)            # Any text before the =, matched few as possible
      //  (?:=)                     # Get the = now
      //  (?&lt;Value&gt;[^\r\n]*)        # Get everything that is not an Line Changes
      //  (?:[\r\n]{0,4})           # MBDC \r\n
      //  )*                        # End Capture groups";
  
      // Parse each file into a Dictionary.
      Dictionary&lt;string, Dictionary&lt;string, string&gt;&gt; iniFile
                      = (from Match m in Regex.Matches(data, pattern, RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline)
                         select new
                         {
                             Section = m.Groups["Section"].Value,
  
                             kvps = (from cpKey in m.Groups["Key"].Captures.Cast&lt;Capture&gt;().Select((a, i) =&gt; new { a.Value, i })
                                     join cpValue in m.Groups["Value"].Captures.Cast&lt;Capture&gt;().Select((b, i) =&gt; new { b.Value, i }) on cpKey.i equals cpValue.i
                                     select new KeyValuePair&lt;string, string&gt;(cpKey.Value, cpValue.Value)).ToDictionary(kvp =&gt; kvp.Key, kvp =&gt; kvp.Value)
  
                         }).ToDictionary(itm =&gt; itm.Section, itm =&gt; itm.kvps);
  
      return iniFile;
  }
  catch (ArgumentException ex)
  {
      System.Diagnostics.Debug.Write(ex.ToString());
      return new Dictionary&lt;string, Dictionary&lt;string, string&gt;&gt;();
  }
  </code></pre>
  
  <p>Thanks in advance.</p>