以下是将集合类转换成DataTable
// <summary> /// 将集合类转换成DataTable /// </summary> /// <param name="list">集合</param> /// <returns></returns> public static DataTable ToDataTable(IList list) { DataTable result = new DataTable(); if (list.Count > 0) { PropertyInfo[] propertys = list[0].GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { result.Columns.Add(pi.Name, pi.PropertyType); } for (int i = 0; i < list.Count; i++) { ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in propertys) { object obj = pi.GetValue(list[i], null); tempList.Add(obj); } object[] array = tempList.ToArray(); result.LoadDataRow(array, true); } } return result; } /// <summary> /// 将泛型集合类转换成DataTable /// </summary> /// <typeparam name="T">集合项类型</typeparam> /// <param name="list">集合</param> /// <returns>数据集(表)</returns> public static DataTable ToDataTable<T>(IList<T> list) { return ConvertX.ToDataTable<T>(list, null); } /// <summary> /// 将泛型集合类转换成DataTable /// </summary> /// <typeparam name="T">集合项类型</typeparam> /// <param name="list">集合</param> /// <param name="propertyName">需要返回的列的列名</param> /// <returns>数据集(表)</returns> public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName) { List<string> propertyNameList = new List<string>(); if (propertyName != null) propertyNameList.AddRange(propertyName); DataTable result = new DataTable(); if (list.Count > 0) { PropertyInfo[] propertys = list[0].GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { if (propertyNameList.Count == 0) { result.Columns.Add(pi.Name, pi.PropertyType); } else { if (propertyNameList.Contains(pi.Name)) result.Columns.Add(pi.Name, pi.PropertyType); } } for (int i = 0; i < list.Count; i++) { ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in propertys) { if (propertyNameList.Count == 0) { object obj = pi.GetValue(list[i], null); tempList.Add(obj); } else { if (propertyNameList.Contains(pi.Name)) { object obj = pi.GetValue(list[i], null); tempList.Add(obj); } } } object[] array = tempList.ToArray(); result.LoadDataRow(array, true); } } return result; }
以下是将DataTable转换为泛型
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Reflection; namespace kang.Code.Extensions { /// <summary> /// Function: 数据扩展类 /// ( /// 使用前引用该命名空间,使用: /// DataRow dr = new BUser().GetUser(); /// MO_user mo_user = dr.ToModel<MO_user>(); /// DataTable dt = new BUser().GetUsers(); /// List<MO_user> list = dt.ToList<Mo_user>(); /// ) /// Author: 蒋康平 /// Date: 2009-9-25 /// </summary> public static class DataExtensions { /// <summary> /// DataRow扩展方法:将DataRow类型转化为指定类型的实体 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <returns></returns> public static T ToModel<T>(this DataRow dr) where T : class, new() { return ToModel<T>(dr, true); } /// <summary> /// DataRow扩展方法:将DataRow类型转化为指定类型的实体 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="dateTimeToString">是否需要将日期转换为字符串,默认为转换,值为true</param> /// <returns></returns> /// <summary> public static T ToModel<T>(this DataRow dr, bool dateTimeToString) where T : class, new() { if (dr != null) return ToList<T>(dr.Table, dateTimeToString).First(); return null; } /// <summary> /// DataTable扩展方法:将DataTable类型转化为指定类型的实体集合 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <returns></returns> public static List<T> ToList<T>(this DataTable dt) where T : class, new() { return ToList<T>(dt, true); } /// <summary> /// DataTable扩展方法:将DataTable类型转化为指定类型的实体集合 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="dateTimeToString">是否需要将日期转换为字符串,默认为转换,值为true</param> /// <returns></returns> public static List<T> ToList<T>(this DataTable dt, bool dateTimeToString) where T : class, new() { List<T> list = new List<T>(); if (dt != null) { List<PropertyInfo> infos = new List<PropertyInfo>(); Array.ForEach<PropertyInfo>(typeof(T).GetProperties(), p => { if (dt.Columns.Contains(p.Name) == true) { infos.Add(p); } }); SetList<T>(list, infos, dt, dateTimeToString); } return list; } #region 私有方法 private static void SetList<T>(List<T> list, List<PropertyInfo> infos, DataTable dt, bool dateTimeToString) where T : class, new() { foreach (DataRow dr in dt.Rows) { T model = new T(); infos.ForEach(p => { if (dr[p.Name] != DBNull.Value) { object tempValue = dr[p.Name]; if (dr[p.Name].GetType() == typeof(DateTime) && dateTimeToString == true) { tempValue = dr[p.Name].ToString(); } try { p.SetValue(model, tempValue, null); } catch { } } }); list.Add(model); } } #endregion } }
代码纯属转帖,感谢原作者的辛勤劳动