【问题标题】:LINQ Group Duplicate Rows and Delete All of Them Except The First oneLINQ 组重复行并删除除第一行之外的所有行
【发布时间】:2012-09-20 00:47:52
【问题描述】:

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

    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

【问题讨论】:

    标签: vb.net linq datatable duplicates


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2010-09-27
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 1970-01-01
      • 2020-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多