0

I have a C# DataTable. I am retrieving Data into DataTable. After that I am trying to DISTINCT entry's at the same time creating a List<MyObject>.

Here is the code with what I am chasing with:

viewModelList = (from item in response.AsEnumerable()
                 select new
                 {
                     description = DataTableOperationHelper.GetStringValue(item, "description"),
                     unitCost = DataTableOperationHelper.GetDecimalValue(item, "unitcost"),
                     defaultChargeable = DataTableOperationHelper.GetBoolValue(item, "defaultChargeable"),
                     contractId = DataTableOperationHelper.GetIntValue(item, "contractID"),
                     consumableid = DataTableOperationHelper.GetIntValue(item, "consumableid")
                 })
                 .Distinct()
                 .Select(x => new ConsumablesViewModel(
                     x.description,
                     x.unitCost,
                     x.defaultChargeable,
                     x.contractId,
                     x.consumableid)
                 )
                 .ToList();

I just want to exclude a single column (consumableid) when I am doing DISTINCT. How could I DISTINCT with my rest of the Data Excluding a single value (consumableid)?

Ocelot20
  • 10,510
  • 11
  • 55
  • 96
Ahmed Zamil
  • 238
  • 2
  • 6
  • 17
  • 1
    take a look at [this answer](http://stackoverflow.com/questions/489258/linq-distinct-on-a-particular-property) – Jonesopolis Apr 28 '14 at 17:32

1 Answers1

0

Take a look at this answered question (LinQ distinct with custom comparer leaves duplicates).

Basically, you create an equality comparer for your type that allows you to decide what makes an object distinct.

Community
  • 1
  • 1
Tony Basallo
  • 3,000
  • 2
  • 29
  • 47