Search code examples
linqdatatablestrongly-typed-dataset

Duplicate Combinations of 3 items In DataTable


I have a situation where I need to look for duplicate combinations of a Guid,String,String coming from a DataTable.

I originally only needed to look for guid, string - and was resolving it using:

        var duplicates = m_SysAdminData.JOBCLASSES.AsEnumerable()
            .Where (x => x.IsINACTIVEDATENull())
            .GroupBy(d => new KeyValuePair<Guid, string>   (d.DEPARTMENTS_GID,d.JOBCLASSESPOSITIONTITLE))
            .Where(d => d.Count() > 1);

How do I modify this to allow for the extra string i need to get involved?


Solution

  • LINQ lets you group by objects of anonymous types, so you can rewrite your query like this:

    var duplicates = m_SysAdminData.JOBCLASSES.AsEnumerable()
            .Where (x => x.IsINACTIVEDATENull())
            .GroupBy(d => new {d.DEPARTMENTS_GID,d.JOBCLASSESPOSITIONTITLE, d.AnotehrString})
            .Where(d => d.Count() > 1);
    

    This replaces the named generic type KeyValuePair<Guid, string> with an anonymous type that has three fields derived from DEPARTMENTS_GID, JOBCLASSESPOSITIONTITLE, and AnotehrString fields of the JOBCLASSES objects.