vb.net linq group 拼接字符串

2013年7月28日

vb.net 的linq 语法与c#的linq语法差异比较大,最近有一个需求,需要实现汇总并且拼接字符串

vb.net 语法如下:

 

Module Module1

Sub Main()

Dim ls As New List(Of Mytest)

ls.Add(New Mytest() With {.Name = “a”, .Tag = “11”})
ls.Add(New Mytest() With {.Name = “a”, .Tag = “12”})

ls.Add(New Mytest() With {.Name = “b”, .Tag = “12”})

ls.Add(New Mytest() With {.Name = “c”, .Tag = “cc”})
ls.Add(New Mytest() With {.Name = “c”, .Tag = “cc”})

Dim rs = From s In ls Group s By s.Name Into g = Group
Select New With {
.name = g.Select(Function(x) x.Name).FirstOrDefault,
.tag = String.Join(“/”, g.Select(Function(x)
Return x.Tag
End Function).ToArray.Distinct)
}

For Each r In rs
Console.WriteLine(r.name & “–” & r.tag)
Next
Console.ReadKey()
End Sub

End Module

Public Class Mytest
Property Name As String = String.Empty
Property Tag As String = String.Empty
End Class

最终运行输出结果:

a–11/12
b–12
c–cc

声明: 本文采用 BY-NC-SA 协议进行授权. 转载请注明转自: vb.net linq group 拼接字符串
本文的评论功能被关闭了.