(转)完美解决方案,可排除DATASET不支持System.Nullable错误
#region ListToDataTable /// <summary> /// ListToDataTable /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list"></param> /// <returns></returns> public static DataTable ToDataTable<T>(this IEnumerable<T> list) { List<PropertyInfo> pList = new List<PropertyInfo>(); Type type = typeof(T); DataTable dt = new DataTable(); Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); }); foreach (var item in list) { DataRow row = dt.NewRow(); pList.ForEach(p => row[p.Name] = p.GetValue(item, null)); dt.Rows.Add(row); } return dt; } #endregion?