将dataTable转换为 List<T>
public class EntityHelper<T> where T : new() { public static List<T> EntityListGet(DataTable table) { List<T> entityList = new List<T>(); if (table != null && table.Rows.Count > 0) { foreach (DataRow dr in table.Rows) { //T model = (T)Activator.CreateInstance(typeof(T)); T entity = new T(); for (int i = 0; i < dr.Table.Columns.Count; i++) { PropertyInfo propertyInfo = entity.GetType().GetProperty(dr.Table.Columns[i].ColumnName); if (propertyInfo != null && dr[i] != DBNull.Value) { propertyInfo.SetValue(entity, dr[i], null); } } entityList.Add(entity); } } return entityList; } }
To be a better man