Convert generic List/Enumerable to DataTable?

后端 未结 27 2145
臣服心动
臣服心动 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:45
    List<YourModel> data = new List<YourModel>();
    DataTable dataTable = Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>(Newtonsoft.Json.JsonConvert.SerializeObject(data));
    
    0 讨论(0)
  • 2020-11-21 23:46

    Se probó el método para que acepte campos con null.

    // remove "this" if not on C# 3.0 / .NET 3.5
        public static DataTable ToDataTable<T>(IList<T> data)
        {
            PropertyDescriptorCollection props =
                TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            Type Propiedad = null;
            for (int i = 0; i < props.Count; i++)
            {
                PropertyDescriptor prop = props[i];
                Propiedad = prop.PropertyType;
                if (Propiedad.IsGenericType && Propiedad.GetGenericTypeDefinition() == typeof(Nullable<>)) 
                {
                    Propiedad = Nullable.GetUnderlyingType(Propiedad);
                }
                table.Columns.Add(prop.Name, Propiedad);
            }
            object[] values = new object[props.Count];
            foreach (T item in data)
            {
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = props[i].GetValue(item);
                }
                table.Rows.Add(values);
            }
            return table;
        }
    
    0 讨论(0)
  • 2020-11-21 23:48

    if you have properties in your class this line of code is OK !!

    PropertyDescriptorCollection props =
                TypeDescriptor.GetProperties(typeof(T));
    

    but if you have all public fields then use this:

    public static DataTable ToDataTable<T>(  IList<T> data)
            {
            FieldInfo[] myFieldInfo;
            Type myType = typeof(T);
            // Get the type and fields of FieldInfoClass.
            myFieldInfo = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance
                | BindingFlags.Public);
    
            DataTable dt = new DataTable();
            for (int i = 0; i < myFieldInfo.Length; i++)
                {
                FieldInfo property = myFieldInfo[i];
                dt.Columns.Add(property.Name, property.FieldType);
                }
            object[] values = new object[myFieldInfo.Length];
            foreach (T item in data)
                {
                for (int i = 0; i < values.Length; i++)
                    {
                    values[i] = myFieldInfo[i].GetValue(item);
                    }
                dt.Rows.Add(values);
                }
            return dt;
            }
    

    the original answer is from above , I just edited to use fields instead of properties

    and to use it do this

     DataTable dt = new DataTable();
                dt = ToDataTable(myBriefs);
                gridData.DataSource = dt;
                gridData.DataBind();
    
    0 讨论(0)
  • 2020-11-21 23:49

    I've written a small library myself to accomplish this task. It uses reflection only for the first time an object type is to be translated to a datatable. It emits a method that will do all the work translating an object type.

    Its blazing fast. You can find it here: ModelShredder on GoogleCode

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

    If you are using VB.NET then this class does the job.

    Imports System.Reflection
    ''' <summary>
    ''' Convert any List(Of T) to a DataTable with correct column types and converts Nullable Type values to DBNull
    ''' </summary>
    
    Public Class ConvertListToDataset
    
        Public Function ListToDataset(Of T)(ByVal list As IList(Of T)) As DataTable
    
            Dim dt As New DataTable()
            '/* Create the DataTable columns */
            For Each pi As PropertyInfo In GetType(T).GetProperties()
                If pi.PropertyType.IsValueType Then
                    Debug.Print(pi.Name)
                End If
                If IsNothing(Nullable.GetUnderlyingType(pi.PropertyType)) Then
                    dt.Columns.Add(pi.Name, pi.PropertyType)
                Else
                    dt.Columns.Add(pi.Name, Nullable.GetUnderlyingType(pi.PropertyType))
                End If
            Next
    
            '/* Populate the DataTable with the values in the Items in List */
            For Each item As T In list
                Dim dr As DataRow = dt.NewRow()
                For Each pi As PropertyInfo In GetType(T).GetProperties()
                    dr(pi.Name) = IIf(IsNothing(pi.GetValue(item)), DBNull.Value, pi.GetValue(item))
                Next
                dt.Rows.Add(dr)
            Next
            Return dt
    
        End Function
    
    End Class
    
    0 讨论(0)
  • 2020-11-21 23:51
    public DataTable ConvertToDataTable<T>(IList<T> data)
    {
        PropertyDescriptorCollection properties =
            TypeDescriptor.GetProperties(typeof(T));
    
        DataTable table = new DataTable();
    
        foreach (PropertyDescriptor prop in properties)
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
    
        foreach (T item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
            {
               row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            }
            table.Rows.Add(row);
        }
        return table;
    }
    
    0 讨论(0)
提交回复
热议问题