Skip to main content
ConvertToDataTable
-
- static DataTable ConvertToDataTable<T>(List<T> models)
- {
-
-
- DataTable dataTable = new DataTable(typeof(T).Name);
-
-
- PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
-
-
-
- foreach (PropertyInfo prop in Props)
- {
-
- dataTable.Columns.Add(prop.Name);
- }
-
- foreach (T item in models)
- {
- var values = new object[Props.Length];
- for (int i = 0; i < Props.Length; i++)
- {
-
- values[i] = Props[i].GetValue(item, null);
- }
-
- dataTable.Rows.Add(values);
- }
- return dataTable;
- }
Comments
Post a Comment