Using LINQ to find / remove duplicates

I have a table containing a bunch of duplicates. These are exact duplicates, with the exception of the primary key column, which is an integer identifier column.

Using EF and LINQ, how to find duplicates and delete them, leaving only one copy.

I found duplicates and the amount of each of them using SQL and SSMS. I just don't know where to start LINQ.

Thanks!

+4
source share
2 answers

On 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(); 
+7
source

Set up @Craig Stuntz's answer with the β€œ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
source

Source: https://habr.com/ru/post/1332941/


All Articles