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, DateOfIncident, Description, EmployeeId, IncidentTypeId, and IsAttendanceIncident
I want to remove duplicate entries where DateOfIncident, EmployeeID, IncidentTypeID and IsAttendanceIncident are the same.
I do want to keep one entry. I know how to do this using a Stored Procedure with CTE in SQL, but I cannot figure out how to accomplish this task using a Lambda expression.
This code returns a list excluding my duplicates, but now how do I go about removing those duplicates that are not in this list ?
var query = db.Incidents.Where(x => x.IsAttendanceIncident == "Y").GroupBy(x => new { x.EmployeeId, x.DateOfIncident, x.IsAttendanceIncident })
.Select(x => x.FirstOrDefault());
UPDATE:
So I went ahead and wrote the custom IEqualityComparer. Now how do I remove the incidents in my context that are not in my distinctItems using the id ?
static void Main(string[] args)
{
DALIncidents.AttendanceEntities1 db = new DALIncidents.AttendanceEntities1();
IEnumerable<DALIncidents.Incident> distinctItems = db.Incidents.Where(c => c.IsAttendanceIncident == "Y");
distinctItems = distinctItems.Distinct(new DALIncidents.DistinctIncidentComparer());
Console.ReadLine();
}