TableListConverter.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace NXWMS.Code.Converter
  10. {
  11. /// <summary>
  12. /// 模型转换
  13. /// </summary>
  14. public static class ModelsConvert
  15. {
  16. #region private
  17. /// <summary>
  18. /// 模型转换成Datatable
  19. /// </summary>
  20. /// <typeparam name="T"></typeparam>
  21. /// <param name="collection"></param>
  22. /// <returns></returns>
  23. public static DataTable ModelsToDataTable<T>(IEnumerable<T> collection)
  24. {
  25. var props = typeof(T).GetProperties();
  26. var dt = new DataTable();
  27. dt.Columns.AddRange(props.Where(m => !(m.PropertyType.IsGenericType) && (m.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))).
  28. Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
  29. if (collection.Count() > 0)
  30. {
  31. for (int i = 0; i < collection.Count(); i++)
  32. {
  33. ArrayList tempList = new ArrayList();
  34. foreach (PropertyInfo pi in props)
  35. {
  36. object obj = pi.GetValue(collection.ElementAt(i), null);
  37. tempList.Add(obj);
  38. }
  39. object[] array = tempList.ToArray();
  40. dt.LoadDataRow(array, true);
  41. }
  42. }
  43. return dt;
  44. }
  45. #endregion
  46. /// <summary>
  47. /// Convert a List{T} to a DataTable.
  48. /// </summary>
  49. public static DataTable ToDataTable<T>(IEnumerable<T> items)
  50. {
  51. var tb = new DataTable(typeof(T).Name);
  52. PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
  53. foreach (PropertyInfo prop in props)
  54. {
  55. Type t = GetCoreType(prop.PropertyType);
  56. tb.Columns.Add(prop.Name, t);
  57. }
  58. foreach (T item in items)
  59. {
  60. var values = new object[props.Length];
  61. for (int i = 0; i < props.Length; i++)
  62. {
  63. values[i] = props[i].GetValue(item, null);
  64. }
  65. tb.Rows.Add(values);
  66. }
  67. return tb;
  68. }
  69. /// <summary>
  70. /// Determine of specified type is nullable
  71. /// </summary>
  72. public static bool IsNullable(Type t)
  73. {
  74. return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
  75. }
  76. /// <summary>
  77. /// Return underlying type if type is Nullable otherwise return the type
  78. /// </summary>
  79. public static Type GetCoreType(Type t)
  80. {
  81. if (t != null && IsNullable(t))
  82. {
  83. if (!t.IsValueType)
  84. {
  85. return t;
  86. }
  87. else
  88. {
  89. return Nullable.GetUnderlyingType(t);
  90. }
  91. }
  92. else
  93. {
  94. return t;
  95. }
  96. }
  97. }
  98. }