DataTable 和List 相互转换
最近自从公司的开发环境改了. 从VS2003 改到VS2008 后 用了C# 3.0 的新东西 自己积累的方法就慢慢的多起来了.
由于经常使用以前经常DataTable 现在都使用List<T> 就此需要经常转换.. 用的多了 自然需要写个简单的方法来实现互相转换
由于C#3.0出现了扩展方法 我们可以通过这样一个特性来简化我们的开发.
DataTable 转换为List<T> 的我们可以通过扩展DataTable来简化
public static class DataTableExtensions
{
/// <summary>
/// DataTable 转换为List 集合
/// </summary>
/// <typeparam name="TResult"> 类型 </typeparam>
/// <param name="dt"> DataTable </param>
/// <returns></returns>
public static List < TResult > ToList < TResult > ( this DataTable dt) where TResult : class , new ()
{
// 创建一个属性的列表
List < PropertyInfo > prlist = new List < PropertyInfo > ();
// 获取TResult的类型实例 反射的入口
Type t = typeof (TResult);
// 获得TResult 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表
Array.ForEach < PropertyInfo > (t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != - 1 ) prlist.Add(p); });
// 创建返回的集合
List < TResult > oblist = new List < TResult > ();
foreach (DataRow row in dt.Rows)
{
// 创建TResult的实例
TResult ob = new TResult();
// 找到对应的数据 并赋值
prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null ); });
// 放入到返回的集合中.
oblist.Add(ob);
}
return oblist;
}
}
相反 的也需要List<T> 转换为DataTable 我们可以通过扩展IEnumberable<T> 来实现
/// <summary>
/// 转换为一个DataTable
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static DataTable ToDataTable < TResult > ( this IEnumerable < TResult > value) where TResult : class
{
// 创建属性的集合
List < PropertyInfo > pList = new List < PropertyInfo > ();
// 获得反射的入口
Type type = typeof (TResult);
DataTable dt = new DataTable();
// 把所有的public属性加入到集合 并添加DataTable的列
Array.ForEach < PropertyInfo > (type.GetProperties(), p => { pList.Add(p);dt.Columns.Add(p.Name, p.PropertyType); });
foreach (var item in value)
{
// 创建一个DataRow实例
DataRow row = dt.NewRow();
// 给row 赋值
pList.ForEach(p => row[p.Name] = p.GetValue(item, null ));
// 加入到DataTable
dt.Rows.Add(row);
}
return dt;
}
}
可以通过上面的代码看出 都是主要通过反射 来实现的. 反射的效率似有点慢. 但是可以接受..
当然你也可以通过表达式树 动态的构造Lambda表达式来提高效率..
DataTable 转换为List<T> 的我们可以通过扩展DataTable来简化
public static class DataTableExtensions
{
/// <summary>
/// DataTable 转换为List 集合
/// </summary>
/// <typeparam name="TResult"> 类型 </typeparam>
/// <param name="dt"> DataTable </param>
/// <returns></returns>
public static List < TResult > ToList < TResult > ( this DataTable dt) where TResult : class , new ()
{
// 创建一个属性的列表
List < PropertyInfo > prlist = new List < PropertyInfo > ();
// 获取TResult的类型实例 反射的入口
Type t = typeof (TResult);
// 获得TResult 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表
Array.ForEach < PropertyInfo > (t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != - 1 ) prlist.Add(p); });
// 创建返回的集合
List < TResult > oblist = new List < TResult > ();
foreach (DataRow row in dt.Rows)
{
// 创建TResult的实例
TResult ob = new TResult();
// 找到对应的数据 并赋值
prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null ); });
// 放入到返回的集合中.
oblist.Add(ob);
}
return oblist;
}
}
相反 的也需要List<T> 转换为DataTable 我们可以通过扩展IEnumberable<T> 来实现
/// <summary>
/// 转换为一个DataTable
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static DataTable ToDataTable < TResult > ( this IEnumerable < TResult > value) where TResult : class
{
// 创建属性的集合
List < PropertyInfo > pList = new List < PropertyInfo > ();
// 获得反射的入口
Type type = typeof (TResult);
DataTable dt = new DataTable();
// 把所有的public属性加入到集合 并添加DataTable的列
Array.ForEach < PropertyInfo > (type.GetProperties(), p => { pList.Add(p);dt.Columns.Add(p.Name, p.PropertyType); });
foreach (var item in value)
{
// 创建一个DataRow实例
DataRow row = dt.NewRow();
// 给row 赋值
pList.ForEach(p => row[p.Name] = p.GetValue(item, null ));
// 加入到DataTable
dt.Rows.Add(row);
}
return dt;
}
}
可以通过上面的代码看出 都是主要通过反射 来实现的. 反射的效率似有点慢. 但是可以接受..
当然你也可以通过表达式树 动态的构造Lambda表达式来提高效率..
扩展方法(1) DataTable 和List 相互转换
作者: Leo_wl
出处: http://www.cnblogs.com/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息查看更多关于DataTable 和List 相互转换的详细内容...