Best way to Find Duplicate entries by a LINQ Group By RRS feed

  • Question

  • User-1188570427 posted

    I have an import that comes into me with First Name, Last Name, and Department.

    I have to find a user id based on this.  For the most part this isn't a good way to go about it, but that is all the import has so we have to deal with it.

    I need to find a quick way to pull back all names that are in my user table where the first name / last name / department is all the same so I can email the admin and let them know there is no way to find a user name. Now most of the time there will not be a user, in the same department, who have the same first name, last name, and department. If there is though I need to know

    So I am trying to find the best way to pull the data back using LINQ and a group by or something like that.

    Thursday, July 9, 2015 8:12 AM


  • User281315223 posted

    I suppose you could consider performing a GROUP BY call and concatenate the FirstName, LastName and Department as your grouping constraint. This would essentially allow you to check if there was more than a single user that matches that constraint :

    var groups = YourUsersCollection.GroupBy(u => String.Format("{0}-{1}-{2}",u.FirstName,u.LastName,u.Department);
    // Find any groups with duplicate names (or multiple matches)
    var dupeGroups = groups.Where(g => g.Count() > 1);

    You should also be able to group by multiple properties without concatenating using an object-based selector :

    var groups = YourUsersCollection.GroupBy(u => new { u.FirstName,u.LastName,u.Department });
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 9, 2015 8:31 AM