如何从 vb.net 中的数据视图值使用分组依据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22894836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Group by from a Dataview values in vb.net
提问by jyothis
is there any way to use group by on datatview values.i can sort the values in dataview but how to group the data in data view (in vb.net) i want to apply the aggregate function on a dataview how make it possible?
有什么方法可以在 datatview 值上使用 group by。我可以对 dataview 中的值进行排序,但是如何在数据视图中对数据进行分组(在 vb.net 中)我想在 dataview 上应用聚合函数如何使它成为可能?
回答by Crono
You can group objects from a DataViewor any other data collection using the GroupBymethod extension.
您可以DataView使用GroupBy方法扩展对一个或任何其他数据集合中的对象进行分组。
For a DataView you'd first have to cast it as IEnumerable(Of DataRowView). Here's a very simplistic example:
对于 DataView,您首先必须将其转换为IEnumerable(Of DataRowView). 这是一个非常简单的例子:
Dim groupedRows =
myDataView.Cast(Of DataRowView).GroupBy(Function(r) r("MyField"))
If you're using a typed DataSetyou might find it much easier to group on typed DataRowinstead as you'll be able to use hard properties for grouping rather than Objectvalues obtained with string references:
如果您使用的是 typed,DataSet您可能会发现在 typed 上分组更容易,DataRow因为您将能够使用硬属性进行分组,而不是Object使用字符串引用获得的值:
Dim groupedRows =
myDataTable.GroupBy(function(r) r.MyField)

