Create Dictionary with LINQ and avoid "item with the same key has already been added" error
You can use GroupBy
to create unique keys:
Dictionary<string, MyObject> objectDict = csvEntries
.Select(csvEntry => csvEntry.ToMyObject())
.GroupBy(x => x.UniqueKey)
.ToDictionary(grp => grp.Key, grp => grp.First());
However, instead of grp.First()
you could create a collection with ToList
or ToArray
. On that way you don't take an arbitary object in case of duplicate keys.
Another option is to use a Lookup<TKey, TValue>
which allows duplicate keys and even non-existing keys, you get an empty sequence in that case.
var uniqueKeyLookup = csvEntries
.Select(csvEntry => csvEntry.ToMyObject())
.ToLookup(x => x.UniqueKey);
IEnumerable<MyObject> objectsFor1234 = uniqueKeyLookup["1234"]; // empty if it doesn't exist
Building on Rango's answer, here's an extension method you can use so you don't need to duplicate the implementation throughout your project:
public static class DictionaryExtensions
{
public static Dictionary<TKey, TValue> ToDictionaryWithDupSelector<TKey, TValue>(
this IEnumerable<TValue> enumerable,
Func<TValue, TKey> groupBy, Func<IEnumerable<TValue>, TValue> selector = null) {
if (selector == null)
selector = new Func<IEnumerable<TValue>, TValue>(grp => grp.First());
return enumerable
.GroupBy(e => groupBy(e))
.ToDictionary(grp => grp.Key, grp => selector(grp));
}
}
By default it will choose the first element when there are duplicates, but I have provided an optional parameter where you can specify an alternate selector. Example call to the extension method:
var objList = new List<string[]> {
new string[2] {"1", "first"},
new string[2] {"1", "last"},
new string[2] {"2", "you"},
};
var asDict = objList.ToDictionary(
arr => arr[0],
grp => grp.Last()
);