title: Creating a dictionary using group-by and a where clause
body: <p>We have a piece of code which retrieves a list of errors from a response returned from an api call. Until now, in this call, the object returned always had a unique list of "itemIds". So we pulled out what we needed into a dictionary as follows</p>
  
  <pre><code>var errorsReturned = accountReports
                      .SelectMany( o =&gt; o.OfferLogList)
                      .Where(o -&gt; !successfulOffers.Contains(o.ItemId))
                      .ToDictionary(o =&gt; o.ItemId, o =&gt; o.PropertyList);
  </code></pre>
  
  <p>"successfulOffers" is a list of itemIds which were successful so we want to ignore these. This gave a dictionary where the key was the id (a string) and the value was the "property list" which was an array of property logs. (We use this later to pull out other info.)</p>
  
  <p>We are now in a position where we may get an object where an ItemId is returned multiple times. This would throw a "same key has already been added" exception using the code above, so I would like to group the offer log list by the ItemId and then flatten out the arrays of property logs. I've come up with the below code but I am not very experienced with linq, particularly with using group by</p>
  
  <pre><code>var errorsReturned = accountReports
                      .SelectMany(o =&gt; o.OfferLogList)
                      .Where(s =&gt; !successfulOffers.Contains(s.ItemId))
                      .GroupBy(o =&gt; o.ItemId)
                      .ToDictionary(o =&gt; o.Key, o =&gt; o.Select(p =&gt; p.PropertyList).SelectMany(p =&gt; p).ToArray());
  </code></pre>
  
  <p>So my question is if anybody can see perhaps a more efficient way of doing this? I have tested this against  the old call and as far as I can see they both return the same result... but again as I am not very experienced in linq I am asking if anybody can see something amiss here.... this is difficult to test in our lower environments so it will hit our production system with a little less testing than I would like, hence the caution!</p>
  
  <p>Thanks in advance.</p>