DataSet Group Implementing in VB.NET

We know a DataSet does not support GROUP BY and recently I needed GROUP BY in one of my applications. This attached class shows how to implement GROUP BY in a DataSet.
  • 7227
 

Recently I needed GROUP BY feature applied to a DataSet in VB.NET. I found the following article Implement a DataSet GROUP BY Helper Class in Visual C# .NET on MSDN, which was very useful.

Nipun Tomar helped me converting C# code to VB.NET, which is attached in this article. This is what CreateGroupByTable method looks like. See the attached source code for more details.

Public Function CreateGroupByTable(ByVal TableName As String, ByVal SourceTable As DataTable, ByVal FieldList As String) As DataTable

  If FieldList Is Nothing Then
    Throw
New ArgumentException("You must specify at least one field in the field list."
)
  Else
    Dim dt As DataTable = New DataTable(TableName)
    ParseGroupByFieldList(FieldList)

    For Each Field As FieldInfo In GroupByFieldInfo
       Dim dc As DataColumn = SourceTable.Columns(Field.FieldName)
       If Field.Aggregate Is Nothing
Then
         dt.Columns.Add(Field.FieldAlias, dc.DataType, dc.Expression)
       Else
         dt.Columns.Add(Field.FieldAlias, dc.DataType)
       End If
    Next

  If Not (ds Is Nothing) Then
    
ds.Tables.Add(dt)
  End If

  Return dt
End If
End
Function

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.