C#DataTable与Model互转
1 /// <summary> 2 /// 实体转换辅助类 3 /// </summary> 4 public class ModelConvertHelper<T> where T : new() 5 { 6 /// <summary> 7 /// List泛型转换DataTable. 8 /// </summary> 9 public DataTable ListToDataTable<T>(List<T> items) 10 { 11 var tb = new DataTable(typeof(T).Name); 12 13 PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); 14 15 foreach (PropertyInfo prop in props) 16 { 17 Type t = GetCoreType(prop.PropertyType); 18 tb.Columns.Add(prop.Name, t); 19 } 20 21 foreach (T item in items) 22 { 23 var values = new object[props.Length]; 24 25 for (int i = 0; i < props.Length; i++) 26 { 27 values[i] = props[i].GetValue(item, null); 28 } 29 30 tb.Rows.Add(values); 31 } 32 33 return tb; 34 } 35 36 /// <summary> 37 /// model转换DataTable 38 /// </summary> 39 /// <typeparam name="T"></typeparam> 40 /// <param name="items"></param> 41 /// <returns></returns> 42 public DataTable ModelToDataTable<T>(T items) 43 { 44 var tb = new DataTable(typeof(T).Name); 45 46 PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); 47 48 foreach (PropertyInfo prop in props) 49 { 50 Type t = GetCoreType(prop.PropertyType); 51 tb.Columns.Add(prop.Name, t); 52 } 53 54 55 var values = new object[props.Length]; 56 57 for (int i = 0; i < props.Length; i++) 58 { 59 values[i] = props[i].GetValue(items, null); 60 } 61 62 tb.Rows.Add(values); 63 64 65 return tb; 66 } 67 68 /// <summary> 69 /// Determine of specified type is nullable 70 /// </summary> 71 public static bool IsNullable(Type t) 72 { 73 return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)); 74 } 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 /// <summary> 99 /// DataTable转换泛型List 100 /// </summary> 101 /// <param name="dt"></param> 102 /// <returns></returns> 103 public static List<T> DataTableToList(DataTable dt) 104 { 105 // 定义集合 106 List<T> ts = new List<T>(); 107 108 // 获得此模型的类型 109 Type type = typeof(T); 110 string tempName = ""; 111 foreach (DataRow dr in dt.Rows) 112 { 113 T t = new T(); 114 // 获得此模型的公共属性 115 PropertyInfo[] propertys = t.GetType().GetProperties(); 116 foreach (PropertyInfo pi in propertys) 117 { 118 tempName = pi.Name; // 检查DataTable是否包含此列 119 120 if (dt.Columns.Contains(tempName)) 121 { 122 // 判断此属性是否有Setter 123 if (!pi.CanWrite) continue; 124 125 object value = dr[tempName]; 126 if (value != DBNull.Value) 127 pi.SetValue(t, value, null); 128 } 129 } 130 ts.Add(t); 131 } 132 return ts; 133 } 134 135 136 public static T DataTableToModel(DataTable dt) 137 { 138 // 定义实体 139 T t = new T(); 140 141 // 获得此模型的类型 142 Type type = typeof(T); 143 string tempName = ""; 144 145 foreach (DataRow dr in dt.Rows) 146 { 147 148 // 获得此模型的公共属性 149 PropertyInfo[] propertys = t.GetType().GetProperties(); 150 foreach (PropertyInfo pi in propertys) 151 { 152 tempName = pi.Name; // 检查DataTable是否包含此列 153 154 if (dt.Columns.Contains(tempName)) 155 { 156 // 判断此属性是否有Setter 157 if (!pi.CanWrite) continue; 158 159 object value = dr[tempName]; 160 if (value != DBNull.Value) 161 pi.SetValue(t, value, null); 162 } 163 } 164 break; 165 } 166 return t; 167 } 168 }
目前学习.NET Core 最好的教程 .NET Core 官方教程 ASP.NET Core 官方教程