我正在尝试将以下内容转换为vb.net。提前致谢
Categories.DataSource = objDT.Rows.Cast<DataRow>()
.Select(r => new { Attendee = r.Field<string>("Attendee"), Item = r.Field<string>("Item") })
.GroupBy(v => v.Attendee)
.Select(g => new { Attendee = g.Key, Item = g.ToList() });
这是我陷入困境的地方,我尝试了两种不同的方法,但仍然无效:
Categories.DataSource = objDT.AsEnumerable() _
.Select(Function(r) New With {.Attendee = r.Field(Of String)("Attendee"), .Item = r.Field(Of String)("Item")}) _
.GroupBy(Function(v) v.Field(Of String)("Attendee")) _
.Select(Function(g) Attendee = g.Key)
或
Categories.DataSource = objDT.Rows.Cast(Of DataRow)().AsEnumerable _
.Select New Object(){ Function(r As DataRow) Attendee = r.Field(Of String)("Attendee"), Item = r.Field(Of String)("Item")} _
.GroupBy( Function(v) v.Category) _
.Select( Function(g) new { Category = g.Key, Numbers = g.ToList() }
答案 0 :(得分:1)
试试这个:
Categories.DataSource = objDT.Rows.Cast(Of DataRow)().Select(Function(r) New With { _
.Attendee = r.Field(Of String)("Attendee"), _
.Item = r.Field(Of String)("Item") _
}).GroupBy(Function(v) v.Attendee).Select(Function(g) New With { _
.Attendee = g.Key, _
.Item = g.ToList() _
})
对象类(New Object()与{})不同于匿名类型(New With {})。
您以后可以使用此网站:http://www.developerfusion.com/tools/convert/csharp-to-vb/。它适用于大多数转换。
答案 1 :(得分:0)
尝试以下
Categories.DataSource = objDT.Rows.Cast(Of DataRow)() _
.Select(Function(r) New With {.Attendee = r.Field(Of String)("Attendee"), .Item = r.Field(Of String)("Item")}) _
.GroupBy(Function(v) v.Attendee) _
.Select(Function(g) New With { .Attendee = g.Key, .Item = g.ToList()})