0

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()?

PushCode
  • 1,419
  • 3
  • 15
  • 31
  • 1
    How many properties are involved in determining if it's distinct? Is it just the key? – Dave Feb 24 '16 at 22:25
  • Yes, Just the key. I need get all the distinct product_name with the corresponding type_id (this can be first the first occurrence of the key). – PushCode Feb 24 '16 at 22:27

2 Answers2

4

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);
Tamas Ionut
  • 4,240
  • 5
  • 36
  • 59
1

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

Community
  • 1
  • 1
NWard
  • 2,016
  • 2
  • 20
  • 24