1. 程式人生 > >將List實體轉換成DataTable

將List實體轉換成DataTable

public static DataTable ToDataTable<T>(IList<T> data)         {             PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));             DataTable dt = new DataTable();             for (int i = 0; i < properties.Count; i++)             {                 PropertyDescriptor property = properties[i];
                dt.Columns.Add(property.Name, property.PropertyType);             }             object[] values = new object[properties.Count];             foreach (T item in data)             {                 for (int i = 0; i < values.Length; i++)                 {                     values[i] = properties[i].GetValue(item);
                }                 dt.Rows.Add(values);             }             return dt;         }