LINQ过滤重复的列值并替换重复值列

时间:2018-05-14 10:48:40

标签: vb.net

我有这张桌子:

Code   ------     Version   
AAA    ------      0.0.0.0    
AAA     ------     0.0.0.1    
AAA     ------     0.0.0.1   
AAA     ------     0.0.0.1   
AAA     ------     0.0.0.1   
BBB    ------      0.0.0.0    
CCC     ------     0.0.0.0    
CCC     ------     0.0.0.0    

这是我在表格中的数据,我想将其转换为:

Code    ------     Version   
AAA    ------      0.0.0.0    
AAA    ------      0.0.0.1    
AAA    ------      0.0.0.2   
AAA    ------      0.0.0.3   
AAA    ------      0.0.0.4  
BBB    ------      0.0.0.0    
CCC   ------       0.0.0.0    
CCC   ------       0.0.0.1    

如今,我正在尝试用外观进行一些测试,但是,我认为它可能会通过LINQ进行改进。这是我目前的代码:

For Each row As DataRow In duplicateEntityRecords.Rows
    Dim strkey = row(0).ToString
    foundrows = dataObject.Tables("tables").Select("version= '" & strkey & "'")
    If foundrows.Length > 1 AndAlso Not strkey.Equals(String.Empty) Then 
        Dim increment As String = "1"
        For i As Integer = 1 To
            foundrows.GetUpperBound(0)
            foundrows(i)("friendlyname") = foundrows(i)("friendlyname") + increment increment = increment + 1
        Next
    End If
Next

这可以用LINQ执行相同的任务吗?

1 个答案:

答案 0 :(得分:0)

您可以按匿名类型进行分组,然后使用First每组只获得一个:

Dim query = From row In table
            Group row By row.Code, row.Version Into codeVersionGroup = Group
            Select codeVersionGroup.First()

For Each x In query
    Console.WriteLine("{0}---{1}",x.Code, x.Version)
Next