DataTableExtension.cs 701 B

123456789101112131415161718192021222324252627
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace System.Data
  6. {
  7. public static class DataTableExtension
  8. {
  9. public static IEnumerable<T> ToEntity<T>(this DataTable table) where T : class, new()
  10. {
  11. if (table != null && table.Rows.Count > 0)
  12. {
  13. var json = JsonConvert.SerializeObject(table);
  14. try
  15. {
  16. return JsonConvert.DeserializeObject<IEnumerable<T>>(json);
  17. }
  18. catch (Exception e)
  19. {
  20. return null;
  21. }
  22. }
  23. return null;
  24. }
  25. }
  26. }