List转DataTable: public static DataTable ToDataTable<T>(IEnumerable<T> collection) { var props = typeof(T).GetProperties(); var dt = new DataTable(); dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray()); )…
using System; using System.Collections.Generic; using System.Data; using System.Reflection; namespace CommonSD { /// <summary> /// DataTable与实体类互相转换 /// </summary> /// <typeparam name="T">实体类</typeparam> public class Mode…
在开发中,把查询结果以DataTable返回很方便,但是在检索数据时又很麻烦,没有list<T>检索方便.但是数据以ILIST形式返回,就为我们在.NET中使用传统的数据绑定造成了不便.下面介绍datatable和list的相互转换 1.datatable转换为list public List<T> GetList<T>(DataTable table) { List<T> list = new List<T>(); T t = default(…
public static DataTable ToDataTableTow(IList list) { DataTable result = new DataTable(); ) { PropertyInfo[] propertys = list[].GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { result.Columns.Add(pi.Name, pi.PropertyType); } ; i < l…