Reputation: 1439
I am trying to convert a DataTable
to a Dictionary of string and integer. DataTable has duplicates and I had written the following code:
Dictionary<string, short> data = dt.AsEnumerable()
.Select(item => new {
Key = item.Field<string>("product_name"),
Value = item.Field<short>("type_id")
})
.Distinct()
.ToDictionary(dr => dr.Key, dr => dr.Value, StringComparer.OrdinalIgnoreCase);
When I have data like this:
Product1 1
Product1 2
Product2 3
Product4 3
the result should be this:
Product1 1
Product2 3
Product4 3
My code is erroring out as the Distinct() considers all the available properties in the anonymous type. Is there a way to achieve this linq or overwrite the default behavior of Distinct()?
Upvotes: 0
Views: 165
Reputation: 4410
Try this (distinct by Key):
Dictionary<string, short> data = dt.AsEnumerable()
.Select(item => new {
Key = item.Field<string>("product_name"),
Value = item.Field<short>("type_id")
})
.GroupBy(x=>x.Key)
.Select(x=>x.First())
.ToDictionary(dr => dr.Key, dr => dr.Value, StringComparer.OrdinalIgnoreCase);
Upvotes: 4
Reputation: 2086
You can provide an implementation of IEqualityComparer to Distinct()
as per the docs: https://msdn.microsoft.com/library/bb338049(v=vs.100).aspx
You can create such an equality comparer for your anonymous type as described in this stack overflow answer: https://stackoverflow.com/a/1071637/1675729
Upvotes: 1