使用Lambda删除重复项

我需要一些帮助,使用lambda表达式删除我的Entity Framework上下文中的重复条目。 我有一个包含以下列的表:

Id, DateOfIncident, Description, EmployeeId, IncidentTypeId, and IsAttendanceIncident

我想删除DateOfIncident, EmployeeID, IncidentTypeID and IsAttendanceIncident are the same.重复条目DateOfIncident, EmployeeID, IncidentTypeID and IsAttendanceIncident are the same. 我确实希望保留一个条目。 我知道如何在SQL中使用带有CTE的存储过程来完成此操作,但我无法弄清楚如何使用Lambda表达式完成此任务。

此代码返回一个不包括我的重复项的列表,但现在我该如何删除不在此列表中的重复项?

  var query = db.Incidents.Where(x => x.IsAttendanceIncident == "Y").GroupBy(x => new { x.EmployeeId, x.DateOfIncident, x.IsAttendanceIncident }) .Select(x => x.FirstOrDefault()); 

更新:

所以我继续编写自定义IEqualityComparer。 现在,如何使用id删除不在我的distinctItems中的上下文中的事件?

  static void Main(string[] args) { DALIncidents.AttendanceEntities1 db = new DALIncidents.AttendanceEntities1(); IEnumerable distinctItems = db.Incidents.Where(c => c.IsAttendanceIncident == "Y"); distinctItems = distinctItems.Distinct(new DALIncidents.DistinctIncidentComparer()); Console.ReadLine(); } 

 var query = db.Incidents .Where(x => x.IsAttendanceIncident == "Y") .GroupBy(x => new { x.EmployeeId, x.DateOfIncident, x.IsAttendanceIncident }) 

例1:

  .Select(x => x.FirstOrDefault()); // your original code which retrieves entities to not delete var dupes = db.Incidents.Except( query ); // get entities to delete 

例2:

  .SelectMany( x => x.OrderBy( y => y.Id ).Skip(1) ); // gets dupes directly var dupes = query; // already have what we need 

最后:

 foreach( var dupe in dupes ) { db.Incidents.Remove( dupe ); } 

从我之前使用的测试上下文生成的示例SQL,其中Person实体与watch具有1:N的关系:

C#:

 context.Persons.SelectMany(x => x.Watches.OrderBy(y => y.Id).Skip(1)) 

生成的SQL:

 SELECT 1 AS [C1], [Skip1].[Id] AS [Id], [Skip1].[Brand] AS [Brand], [Skip1].[Person_Id] AS [Person_Id] FROM [dbo].[Persons] AS [Extent1] CROSS APPLY (SELECT [Project1].[Id] AS [Id], [Project1].[Brand] AS [Brand], [Project1].[Person_Id] AS [Person_Id] FROM ( SELECT [Project1].[Id] AS [Id], [Project1].[Brand] AS [Brand], [Project1].[Person_Id] AS [Person_Id], row_number() OVER (ORDER BY [Project1].[Id] ASC) AS [row_number] FROM ( SELECT [Extent2].[Id] AS [Id], [Extent2].[Brand] AS [Brand], [Extent2].[Person_Id] AS [Person_Id] FROM [dbo].[Watches] AS [Extent2] WHERE [Extent1].[Id] = [Extent2].[Person_Id] ) AS [Project1] ) AS [Project1] WHERE [Project1].[row_number] > 1 ) AS [Skip1] 

您将需要使用Distinctfunction,如果您只想要一些字段,则需要创建Equality Comparer。 (的IEqualityComparer)

啊刚刚看到上面的评论,请查看更多内容:

使用linq删除列表中的重复项

 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现在只包含重复项?

 var query = db.Incidents.Where(x => x.IsAttendanceIncident == "Y").GroupBy(x => new { x.EmployeeId, x.DateOfIncident, x.IsAttendanceIncident }) .SelectMany(x => x.Skip(1));