Using LINQ to find / delete duplicates

后端 未结 2 1939
野趣味
野趣味 2020-12-18 05:26

I have a table that contains a bunch of duplicates. These are exact duplicates, minus the primary key column, which is an integer identity column.

Using EF and LINQ,

相关标签:
2条回答
  • 2020-12-18 05:43

    Off the top of my head (untested):

    var q = from r in Context.Table
            group r by new { FieldA = r.FieldA, FieldB = r.FieldB, // ...
                into g
            where g.Count() > 1
            select g;
    foreach (var g in q)
    {
        var dupes = g.Skip(1).ToList();
        foreach (var record in dupes)
        {
            Context.DeleteObject(record);
        }
    }
    Context.SaveChanges();
    
    0 讨论(0)
  • 2020-12-18 05:47

    Building on @Craig Stuntz's answer with a "one liner" alternative:

    var duplicates = db.Table.GroupBy(a => new { a.FieldA, a.FieldB, ...})
                             .Where(a => a.Count() > 1)
                             .SelectMany(a => a.ToList());
    
    foreach (var d in duplicates)
    {
         db.DeleteObject(d);
    }
    
    db.SaveChanges();
    
    0 讨论(0)
提交回复
热议问题