1

I'd like to find duplicates by name value in a list. I've included an example below of what I'm trying to achieve.

I can output the duplicate name value if I loop over query, however I'd also like to output the Address and ID values into a table.

Can this be done?

public class Record
{
    public int Id { get; set; }
    public string Name{ get; set; }
    public string Address{ get; set; }
}

I find duplicates using the query below:

var query = record.GroupBy(s => s.name).Where(s => s.Count() > 1);
Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
Johnny Grimes
  • 411
  • 7
  • 17

4 Answers4

3

If you want to simply return all of the records that form duplicates with at least one other record by name then you almost had it right.

Just do this:

var query =
    record
        .GroupBy(s => s.Name)
        .Where(s => s.Count() > 1)
        .SelectMany(r => r);

So, if I start with this:

var record = new[]
{
        new { Id = 1, Name = "Joe", Address = "X" },
        new { Id = 2, Name = "Barry", Address = "Y"},
        new { Id = 3, Name = "Joe", Address = "A"},
        new { Id = 4, Name = "Mike", Address = "B"},
        new { Id = 5, Name = "Barry", Address = "B"},
        new { Id = 6, Name = "Joe", Address = "B"},
};

I get out this:

query

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
0

The following works for me:

var query = records.GroupBy(s => s.Name)
        .Where(s => s.Count() > 1)
        .SelectMany(x => x.Select(y => new Record{ Id = y.Id, Name = y.Name, Address = y.Address}));

Test with

var records = new []  {
            new Record {Address = "A", Id = 1, Name = "Tim"},
            new Record {Address = "B", Id = 2, Name = "Tim"},
            new Record {Address = "C", Id = 3, Name = "Tim"},
            new Record {Address = "D", Id = 4, Name = "Hans"}
        };

Gets the first three Records named Tim.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
0

There are two ways of approaching this:

  1. Output duplicates for each group
  2. 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.

RePierre
  • 9,358
  • 2
  • 20
  • 37
-1

This should work (Not tested):

var grouped = record.GroupBy(s => s.Name).Where(s => s.Count() > 1);
var result = grouped.SelectMany(c => c.Select(p => p.Address));
Jannik
  • 2,310
  • 6
  • 32
  • 61