Delete duplicates using Lambda

后端 未结 4 1555
南笙
南笙 2021-01-23 01:23

I need some help using a lambda expression to remove duplicate entries in my Entity Framework context. I have a table with the following columns:

Id, DateOfIncide

4条回答
  •  清酒与你
    2021-01-23 01:59

    var query = db.Incidents.Where(x => x.IsAttendanceIncident == "Y")
                    .GroupBy(x => new { x.Id, x.EmployeeId, x.DateOfIncident, x.IsAttendanceIncident })
                    .Select(x => x.FirstOrDefault());
    
    
    var query2 = from duplicate in db.Incidents
                     .Where(x => x.IsAttendanceIncident == "Y" && !query.Any(i => i.Id == duplicate.Id));
    

    query2 will now just contain the duplicates?

提交回复
热议问题