I have a query which deletes duplicate records from my database table.
Can anyone please assist in converting it to Linq:
DELETE FROM SSRecs
WHERE (SSRecId NOT IN
(SELECT MAX(SSRecId) AS Expr1
FROM SSRecs AS SSRecs_1
GROUP BY ref, DateTime))
I think I can get the same list of duplicates from this query:
var duplicates = (from check in db.SSRecs
group check by new { check.ref, check.DateTime } into g
where g.Count() > 1
select g.Key).ToList();
This is where I'm a little stuck - I've got the list of duplicate Keys (SSRecId) in the "duplicates" list - but how do I bulk remove them from the database - this is my pseudo code:
db.SSRecs.Where(x => x.SSRecId ** is in duplicates list **)
.ToList()
.ForEach(db.SSRecs.DeleteObject);
db.SaveChanges();
Thanks for any guidance, Mark