2012-09-20 33 views
-1

我有以下LINQ代碼,我發現在一個關於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 

回答

1

可以使用Skip()跳過第一行各組重複的:

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