使用Lambda删除重复项

时间:2013-08-12 12:20:29

标签: c# entity-framework lambda

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

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

我想删除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<DALIncidents.Incident> distinctItems = db.Incidents.Where(c => c.IsAttendanceIncident == "Y");
       distinctItems = distinctItems.Distinct(new DALIncidents.DistinctIncidentComparer());

       Console.ReadLine(); 
    }

4 个答案:

答案 0 :(得分:3)

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]

答案 1 :(得分:0)

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

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

Remove duplicates in the list using linq

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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