Kuntady Nithesh
Kuntady Nithesh

Reputation: 11721

finding duplicates for two columns entries in datatable using LINQ

I have a datatable dt which has supplierId and SubsystemId as 2 of its columns . I want to find if there is any duplicate entries for the combination of these two column values .

I am a starter to Linq . How can I do this ?

Upvotes: 0

Views: 6062

Answers (2)

Kuntady Nithesh
Kuntady Nithesh

Reputation: 11721

  var dups = from row in dt.Copy().AsEnumerable()
                       group row by new { SubsystemTypeId = row.Field<int>("SubsystemTypeId"), SupplierId = row.Field<int>("SupplierId") }
                           into grp
                           where grp.Count() > 1
                          select grp.Key;      

Thanks SriRam

Upvotes: 0

SRIRAM
SRIRAM

Reputation: 1888

You can find your duplicates by grouping your elements by supplierId and SubsystemId. Do a count on them afterwards and you will find which ones are duplicates.

This is how you would isolate the dupes :

  var duplicates = items.GroupBy(i => new {i.supplierId, i.SubsystemId})
                  .Where(g => g.Count() > 1)
                  .Select(g => g.Key);

Upvotes: 6

Related Questions