LINQ Group重复行并删除所有它们除了第一行

时间:2012-09-20 00:47:52

标签: vb.net linq datatable duplicates

我有以下LINQ代码,我在stackoverflow上的一个问题中找到,我想用它来删除数据表中的重复行并留下索引最少的一行,这里是代码,我无法想象输出要在注释行中添加的代码

    Dim duplicates = From row In GoogleResults.AsEnumerable()
           Let seller = row.Field(Of String)("Seller")
           Group row By seller Into DuplicateSellers = Group
           Where DuplicateSellers.Count() > 1
           Select DuplicateSellers
            For Each DuplicateSellerRows In duplicates
                For Each row In DuplicateSellerRows
                    'remove current row, but skip the first one
'row.Delete() will remove all rows but how can i keep the first one?
                Next
            Next

1 个答案:

答案 0 :(得分:1)

您可以使用Skip()跳过每组重复项中的第一行:

For Each DuplicateSellerRows In duplicates
    For Each row In DuplicateSellerRows.Skip(1)            
        row.Delete() 
    Next
Next