Convert generic List/Enumerable to DataTable?

后端 未结 27 2124
臣服心动
臣服心动 2020-11-21 23:20

I have few methods that returns different Generic Lists.

Exists in .net any class static method or whatever to convert any list into a datatable? The only thing tha

相关标签:
27条回答
  • 2020-11-21 23:40

    I think it's more convenient and easy to use.

       List<Whatever> _lobj= new List<Whatever>(); 
        var json = JsonConvert.SerializeObject(_lobj);
                    DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));
    
    0 讨论(0)
  • 2020-11-21 23:43

    Marc Gravell's answer but in VB.NET

    Public Shared Function ToDataTable(Of T)(data As IList(Of T)) As DataTable
        Dim props As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(T))
        Dim table As New DataTable()
        For i As Integer = 0 To props.Count - 1
                Dim prop As PropertyDescriptor = props(i)
                table.Columns.Add(prop.Name, prop.PropertyType)
        Next
        Dim values As Object() = New Object(props.Count - 1) {}
        For Each item As T In data
                For i As Integer = 0 To values.Length - 1
                        values(i) = props(i).GetValue(item)
                Next
                table.Rows.Add(values)
        Next
        Return table
    End Function
    
    0 讨论(0)
  • 2020-11-21 23:43

    To convert a generic list to data table, you could use the DataTableGenerator

    This library lets you convert your list into a data table with multi-feature like

    • Translate data table header
    • specify some column to show
    0 讨论(0)
  • 2020-11-21 23:43
      private DataTable CreateDataTable(IList<T> item)
            {
                Type type = typeof(T);
                var properties = type.GetProperties();
    
                DataTable dataTable = new DataTable();
                foreach (PropertyInfo info in properties)
                {
                    dataTable.Columns.Add(new DataColumn(info.Name, Nullable.GetUnderlyingType(info.PropertyType) ?? info.PropertyType));
                }
    
                foreach (T entity in item)
                {
                    object[] values = new object[properties.Length];
                    for (int i = 0; i < properties.Length; i++)
                    {
                        values[i] = properties[i].GetValue(entity);
                    }
    
                    dataTable.Rows.Add(values);
                }
                return dataTable;
            }
    
    0 讨论(0)
  • 2020-11-21 23:44

    A 2019 answer if you're using .NET Core - use the Nuget ToDataTable library. Advantages:

    • Better performance than FastMember
    • Also creates structured SqlParameters for use as SQL Server Table-Valued Parameters

    Disclaimer - I'm the author of ToDataTable

    Performance - I span up some Benchmark .Net tests and included them in the ToDataTable repo. The results were as follows:

    Creating a 100,000 Row Datatable:

                               MacOS         Windows
    Reflection                 818.5 ms      818.3 ms
    FastMember from           1105.5 ms      976.4 ms
     Mark's answer
    Improved FastMember        524.6 ms      456.4 ms
    ToDataTable                449.0 ms      376.5 ms
    

    The FastMember method suggested in Marc's answer seemed to perform worse than Mary's answer which used reflection, but I rolled another method using a FastMember TypeAccessor and it performed much better. Nevertheless the ToDataTable package outperformed the lot.

    0 讨论(0)
  • 2020-11-21 23:45

    Another approach is the above:

      List<WhateEver> lst = getdata();
      string json = Newtonsoft.Json.JsonConvert.SerializeObject(lst);
      DataTable pDt = JsonConvert.DeserializeObject<DataTable>(json);
    
    0 讨论(0)
提交回复
热议问题