There are two ways of approaching this:
- Output duplicates for each group
- Load all duplicates in a single table and show them all
For the first thing all you need to do is to iterate over the grouped records:
var duplicates = records.GroupBy(r => r.Name)
.Where(g => g.Count() > 1);
foreach(var duplicateGroup in duplicates)
{
Console.WriteLine("Duplicates for: {0}", duplicateGroup.Key);
foreach(var item in duplicateGroup)
{
Console.WriteLine("Id: {0}, Address: {1}", item.Id, item.Address);
}
}
The code above will output the Name
of the duplicates followed by the Id
and Address
values for each record in a duplicate group.
If you want to load all the duplicates in a table (e.g. to export them to an excel file) you need to use the following:
var duplicates = records.GroupBy(r => r.Name)
.Where(g => g.Count() > 1)
.SelectMany(g => g.ToArray());
foreach(var duplicate in duplicates)
{
Console.WriteLine("Name: {0}, Id: {1}, Address: {2}", duplicate.Name, duplicate.Id, duplicate.Address);
}
This chunk of code will output only the duplicates with the duplicate Name
in the first column so it would be easier to look through the records.