title: Create Dictionary with LINQ and avoid "item with the same key has already been added" error
body: <p>I want to find a key in a dictionary and <strong>replace</strong> the value if it is found or add the key/value if it is not.</p>
  
  <p>Code:</p>
  
  <pre><code>public class MyObject
  {
  
      public string UniqueKey { get; set; }
      public string Field1 { get; set; }
      public string Field2 { get; set; }
  
  }
  </code></pre>
  
  <p>LINQ Solution (throws <code>An item with the same key has already been added.</code>):</p>
  
  <pre><code>Dictionary&lt;string, MyObject&gt; objectDict = csvEntries.ToDictionary(csvEntry =&gt; csvEntry.ToMyObject().UniqueKey, csvEntry =&gt; csvEntry.ToMyObject());
  </code></pre>
  
  <p>ForEach solution (works):</p>
  
  <pre><code>Dictionary&lt;string, MyObject&gt; objectDict = new Dictionary&lt;string, MyObject&gt;();
  foreach (CSVEntry csvEntry in csvEntries)
  {
  
      MyObject obj = csvEntry.ToMyObject();
  
      if (objectDict.ContainsKey(obj.UniqueKey))
      {
          objectDict[obj.UniqueKey] = obj;
      }
      else {
          objectDict.Add(obj.UniqueKey, obj);
      }
  
  }
  </code></pre>
  
  <p>I really liked the LINQ solution but as it stands, it throws the above error. Is there any nice way of avoiding the error and using LINQ?</p>