You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
942 B

2 years ago
  1. using System.Collections.Generic;
  2. using System.Data;
  3. using System.Reflection;
  4. namespace EasyBL
  5. {
  6. public class DataTableExtensions
  7. {
  8. /*Converts List To DataTable*/
  9. public static DataTable ListToDataTable<T>(List<T> items)
  10. {
  11. var dataTable = new DataTable(typeof(T).Name);
  12. var Properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
  13. foreach (PropertyInfo propInfo in Properties)
  14. {
  15. dataTable.Columns.Add(propInfo.Name);
  16. }
  17. foreach (T item in items)
  18. {
  19. var values = new object[Properties.Length];
  20. for (int i = 0; i < Properties.Length; i++)
  21. {
  22. values[i] = Properties[i].GetValue(item, null);
  23. }
  24. dataTable.Rows.Add(values);
  25. }
  26. return dataTable;
  27. }
  28. }
  29. }