我是VB菜鸟,LINQ菜鸟,查了一上午,大都是对List且是C#的
有DATATABLE
id name
---------
a1 b3
a2 b1
a3 b2
我想取得以id分组,取max(name)该怎么写linq
sql的话就是 select max(name) from datatable group by id
------解决方案--------------------------------------------------------
Dim result = datatable1.Rows.Cast(Of DataRow)().GroupBy(Function(x) x("id").ToString()).Select(Function(x) x.Max(Function(y) y["Name"].ToString()))
------解决方案--------------------------------------------------------
一直C#,VB.net真不太熟,费了半天终于成功的搞出来了
Dim da1 As DataTable = New DataTable
da1.Columns.Add("id")
da1.Columns.Add("name")
da1.Columns.Add("su", Type.GetType("System.Decimal"))
Dim dr As DataRow = da1.NewRow()
dr("id") = 1
dr("name") = "张三"
dr("su") = 10.0
da1.Rows.Add(dr)
dr = da1.NewRow()
dr("id") = 1
dr("name") = "张三"
dr("su") = 20.0
da1.Rows.Add(dr)
dr = da1.NewRow()
dr("id") = 2
dr("name") = "张三"
dr("su") = 30.0
da1.Rows.Add(dr)
dr = da1.NewRow()
dr("id") = 1
dr("name") = "李四"
dr("su") = 30.0
da1.Rows.Add(dr)
dr = da1.NewRow()
dr("id") = 1
dr("name") = "李四"
dr("su") = 40.0
da1.Rows.Add(dr)
Dim tem1 = From k In da1.Rows.Cast(Of DataRow)()
Group k By id = k.Field(Of String)("id"), name = k.Field(Of String)("name") Into g = Group, su = Sum(k.Field(Of Decimal)("su"))
Select id, name, su
For Each c In tem1
Console.WriteLine("{0},{1},{2}", c.id, c.name, c.su)