Click here to Skip to main content
15,895,011 members
Articles / Web Development / ASP.NET

Generic List(Of) to DataSet: Three Approaches

Rate me:
Please Sign up or sign in to vote.
2.60/5 (9 votes)
4 Nov 2006CPOL 128.9K   29   14
Turn a List(Of) into a DataSet in three different ways and check performance.

Introduction

This article is a small pilot I wrote to test three ways to convert a List(Of Any Class) into a DataSet. I also included some performance images to show which methods appears to be better.

Conventional

VB
'
' Conventional
'
Private Function GetDataSetConventional(ByVal list As _
        List(Of FileSearchResultItem)) As DataSet
    Dim _result As New DataSet()
    _result.Tables.Add("results")
    _result.Tables("results").Columns.Add("Index")
    _result.Tables("results").Columns.Add("Image")    
    _result.Tables("results").Columns.Add("Row")
    _result.Tables("results").Columns.Add("Name")
    _result.Tables("results").Columns.Add("Size")
    _result.Tables("results").Columns.Add("IsDirectory")
    _result.Tables("results").Columns.Add("Files")

    For Each item As FileSearchResultItem In list
        Dim newRow As DataRow = _
            _result.Tables("results").NewRow()
        newRow("Row") = item.Index
        newRow("Image") = item.Image
        newRow("Name") = item.Name
        newRow("Size") = item.Size
        newRow("Files") = item.Files
        newRow("IsDirectory") = item.IsDirectory
        _result.Tables("results").Rows.Add(newRow)
    Next
    Return _result
End Function

Hacked (Uses Dynamic Methods)

VB
'
' Hacked
'
Private Function GetDataSetHacked(Of T)(ByVal _
        list As List(Of T)) As DataSet    
    Dim _resultDataSet As New DataSet()    
    Dim _resultDataTable As New DataTable("results")
    Dim _resultDataRow As DataRow = Nothing    
    Dim _itemProperties() As PropertyInfo = _
         list.Item(0).GetType().GetProperties()
    Dim _callPropertyValue(_itemProperties.Length()) _
         As _getPropertyDelegate(Of T)

    '
    ' Meta Data.
    ' Each item property becomes a column in the table 
    ' Build an array of Property Getters, one for each Property 
    ' in the item class. Can pass anything as [item] it is just a 
    ' place holder parameter, later we will invoke it with the
    ' correct item. This code assumes the runtime does not change
    ' the ORDER in which the proprties are returned.
    '
    _itemProperties = list.Item(0).GetType().GetProperties()
    Dim i As Integer = 0
    
    For Each p As PropertyInfo In _itemProperties
        _callPropertyValue(i) = _
          CreateGetPropertyValueDelegate(CType(list.Item(0), _
          T), p.Name)
        _resultDataTable.Columns.Add(p.Name, _
                  p.GetGetMethod.ReturnType())
        i += 1    
    Next    
    '    
    ' Data    
    '
    For Each item As T In list        
        '
        ' Get the data from this item into a DataRow
        ' then add the DataRow to the DataTable.
        ' Eeach items property becomes a colunm.
        '
        _itemProperties = item.GetType().GetProperties()        
        _resultDataRow = _resultDataTable.NewRow()
        i = 0        
        For Each p As PropertyInfo In _itemProperties            
            _resultDataRow(p.Name) = _
               _callPropertyValue(i).Invoke(item)
            i += 1
        Next        
        _resultDataTable.Rows.Add(_resultDataRow)    
    Next    
    '    
    ' Add the DataTable to the DataSet, We are DONE!
    '
    _resultDataSet.Tables.Add(_resultDataTable)
    Return _resultDataSet
End Function

'
' Delegate to call DynamicMethod
'
Private Delegate Function _
       _getPropertyDelegate(Of T)(ByVal _
       item As T) As Object
'
' Builds a Dynamic Method to read the peoperty of an object
'
Private Function CreateGetPropertyValueDelegate(Of T)(ByVal _
        item As T, ByVal itemName As Object) As _
        _getPropertyDelegate(Of T)

    Dim _arg() As Type = {GetType(T)}
    Dim _propertyInfo As PropertyInfo = _
        item.GetType().GetProperty(itemName, _
        BindingFlags.Public Or BindingFlags.Instance)
    Dim _getPropertyValue As DynamicMethod = Nothing
    Dim _ilGenerator As ILGenerator = Nothing

    '
    ' Create the funciton
    '
    _getPropertyValue = New DynamicMethod("_getPropertyValue", _
                        GetType(Object), _arg, _
                        GetType(Integer).Module)
    '
    ' Write the body of the function.
    '
    _ilGenerator = _getPropertyValue.GetILGenerator()
    _ilGenerator.Emit(OpCodes.Ldarg_0)
    _ilGenerator.Emit(OpCodes.Callvirt, _propertyInfo.GetGetMethod())
    ' Box value types.
    If Not _propertyInfo.PropertyType.IsClass Then
        _ilGenerator.Emit(OpCodes.Box, _
                     _propertyInfo.GetGetMethod.ReturnType())
    _ilGenerator.Emit(OpCodes.Ret)
    '
    ' Return the Delegate
    '
    Return CType(_getPropertyValue.CreateDelegate(_
                 GetType(_getPropertyDelegate(Of T))), _
                 _getPropertyDelegate(Of T))
End Function

Native (Uses GetValue)

VB
'
' Native
'
Private Function GetDataSetNative(Of T)(ByVal _
        list As List(Of T)) As DataSet
    Dim _resultDataSet As New DataSet()    
    Dim _resultDataTable As New DataTable("results")
    Dim _resultDataRow As DataRow = Nothing    
    Dim _itemProperties() As PropertyInfo = _
         list.Item(0).GetType().GetProperties()    
    '    
    ' Meta Data. 
    '
    _itemProperties = list.Item(0).GetType().GetProperties()
    For Each p As PropertyInfo In _itemProperties
        _resultDataTable.Columns.Add(p.Name, _
                  p.GetGetMethod.ReturnType())
    Next
    '
    ' Data
    '
    For Each item As T In list
        '
        ' Get the data from this item into a DataRow
        ' then add the DataRow to the DataTable.
        ' Eeach items property becomes a colunm.
        '
        _itemProperties = item.GetType().GetProperties()
        _resultDataRow = _resultDataTable.NewRow()
        For Each p As PropertyInfo In _itemProperties
            _resultDataRow(p.Name) = p.GetValue(item, Nothing)
        Next
        _resultDataTable.Rows.Add(_resultDataRow)
    Next
    '
    ' Add the DataTable to the DataSet, We are DONE!
    '
    _resultDataSet.Tables.Add(_resultDataTable)
    Return _resultDataSet
End Function

Performance

Conventional

I used 75,000 records for this test.

Pilot Front End

FrontEnd

I will not comment on the performance much. I ran 5 series for each method, and it appears all things equal, the Hacked IL method runs faster, as much as twice faster. Incredibly, the Conventional method, which is the stupidest of all, does not do that bad as you can see. The question remains, should we use these kinds of tricks cause or should we attach ourselves to more conventional programming and be more compatible with future versions? I leave the reader to decide.

Hope you enjoy it, and please feedback if possible. Thanks!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNative VB.NET given: System.Collections.Generic.IList(Of T) return System.Data.DataSet Pin
Member 1089674025-Feb-16 6:55
Member 1089674025-Feb-16 6:55 
QuestionBugs Pin
Raam0016-Oct-11 5:05
Raam0016-Oct-11 5:05 
Questionxml Pin
maoie28-Sep-11 19:06
maoie28-Sep-11 19:06 
QuestionVB data structures Pin
NotAndrewTurvil16-Aug-11 23:16
NotAndrewTurvil16-Aug-11 23:16 
This is an excellent, well-researched article. But what I'd really like someone to do is a summary of all the possible .NET data structures for VB, with some quick sample code for each and an explanation of which is best.

Currently I use data tables and arrays for holding data (you can tell I've got a VB background). How much am I missing?

Andy
GeneralExcellent article Pin
Member 37196818-Feb-11 11:29
Member 37196818-Feb-11 11:29 
QuestionExcellent, I was trying to do my testing - can we have the code for yours? Pin
Member 287586421-Jun-10 21:00
Member 287586421-Jun-10 21:00 
GeneralError while converting List to DataSet in C#.NET 3.5 Pin
shaileshbhadane16-Nov-09 19:03
shaileshbhadane16-Nov-09 19:03 
GeneralGetting an error when i run the code Pin
Member 136307518-Sep-08 9:22
Member 136307518-Sep-08 9:22 
QuestionTranslate Method to c# Pin
Eowin0620-Apr-07 1:34
Eowin0620-Apr-07 1:34 
GeneralStrangesness Pin
arapallo6-Nov-06 6:14
arapallo6-Nov-06 6:14 
GeneralFeedBack Pin
arapallo6-Nov-06 1:53
arapallo6-Nov-06 1:53 
GeneralMeasurement results Pin
Robert Rohde6-Nov-06 1:11
Robert Rohde6-Nov-06 1:11 
GeneralRe: Measurement results Pin
arapallo6-Nov-06 1:45
arapallo6-Nov-06 1:45 
GeneralRe: Measurement results Pin
arapallo6-Nov-06 1:58
arapallo6-Nov-06 1:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.