Home > Enterprise >  Get duplicates from two columns
Get duplicates from two columns

Time:09-27

I need to get students that have duplicate schools in the list (DEF & PQR) and duplicate age group (21 & 22). Output needs to be cast as var Results= in the original list format (List<Student>).

List<Student> studentList = new List<Student>() { 
    new Student() { StudentID = 1, StudentName = "John", Age = 18, School = ABC } ,
    new Student() { StudentID = 2, StudentName = "Steve",  Age = 21, School = DEF } ,
    new Student() { StudentID = 3, StudentName = "Bill",  Age = 21, School = DEF } ,
    new Student() { StudentID = 4, StudentName = "Josh" , Age = 20, School = DEF },
    new Student() { StudentID = 5, StudentName = "Jack" , Age = 19, School = JKL },
    new Student() { StudentID = 6, StudentName = "Thomas" , Age = 18, School = MNO },
    new Student() { StudentID = 7, StudentName = "Rich" , Age = 22, School = PQR },
    new Student() { StudentID = 8, StudentName = "Robert" , Age = 22, School = PQR},
    new Student() { StudentID = 9, StudentName = "John" , Age = 20, School = PQR },
    new Student() { StudentID = 10, StudentName = "Emma" , Age = 20, School = XYZ };

Output: Steve, Bill, Rich, Robert

What I have tried..

Thanks to Group By Multiple Columns, I used-

var Results= studentList.GroupBy(x => new { x.School, x.Age }).ToList<Student>();

But I get an error:

Cannot convert from 'System.Collections.Generic.IEnumerable<System.Linq.IGrouping<<annonymous type:string School int Age>, Project1.MainWindow.Student>> to System.Collections.Generic<Project1.MainWindow.Student>

I also tried- var Results= studentList.GroupBy(x => new { x.School, x.Age });

But it also didn't work with similar error.

Any help is appreciated.

CodePudding user response:

You're getting an error because GroupBy doesn't return a collection of Students, it returns a list of groupings of Students. Each grouping is defined by a Key, and the collection of matching entries.

The other thing you'll want to add is only include groups that have 2 or more entries - that means that the Key you specify has duplicates in the source collection.

List<Student> studentsWithDuplicateAgeAndSchool = studentList
    .GroupBy(s => new { s.Age, s.School }) // bucket results by Age and School
    .Where(g => g.Count() >= 2) // only include buckets that have 2 or more entries
    .SelectMany(g => g) // for each remaining bucket, flatten the results to one collection
    .ToList(); // execute the query to create a concrete list (optional)

Here's a runnable online version.

  • Related