DataTable转LIst

/// <summary>
/// 利用反射将DataTable转换为List<T>对象
/// </summary>
/// <param name="dt">DataTable 对象</param>
/// <returns>List<T>集合</returns>
public static List<T> DataTableToList<T>(DataTable dt) where T :class,new ()
{
// 定义集合
List<T> ts = new List<T>();
//定义一个临时变量
string tempName = string.Empty;
//遍历DataTable中所有的数据行
foreach (DataRow dr in dt.Rows)
{
T t = new T();
// 获得此模型的公共属性
PropertyInfo[] propertys = t.GetType().GetProperties();
//遍历该对象的所有属性
foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name;//将属性名称赋值给临时变量
//检查DataTable是否包含此列(列名==对象的属性名)
if (dt.Columns.Contains(tempName))
{
//取值
object value = dr[tempName];
//如果非空,则赋给对象的属性
if (value != DBNull.Value)
{
pi.SetValue(t,value,null);
}
}
}
//对象添加到泛型集合中
ts.Add(t);
}
return ts;
}

List转DataSet

 /// <summary>
/// List转换成DataSet
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="list">将要转换的List</param>
/// <returns></returns>
public DataSet ConvertToDataSet<T>(IList<T> list)
{
if (list == null || list.Count <= )
{
return null;
}
DataSet ds = new DataSet();
DataTable dt = new DataTable(typeof(T).Name);
DataColumn column;
DataRow row;
System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (T t in list)
{
if (t == null)
{
continue;
}
row = dt.NewRow();
for (int i = , j = myPropertyInfo.Length; i < j; i++)
{
System.Reflection.PropertyInfo pi = myPropertyInfo[i];
string name = pi.Name;
if (dt.Columns[name] == null)
{
column = new DataColumn(name, pi.PropertyType);
dt.Columns.Add(column);
}
row[name] = pi.GetValue(t, null);
}
dt.Rows.Add(row);
}
ds.Tables.Add(dt);
return ds;
}

List转DataSet

private DataTable ToDataTable<T>(List<T> items)
{
var tb = new DataTable(typeof(T).Name);
PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in props)
{
Type t = GetCoreType(prop.PropertyType);
tb.Columns.Add(prop.Name, t);
}
foreach (T item in items)
{
var values = new object[props.Length];
for (int i = ; i < props.Length; i++)
{
values[i] = props[i].GetValue(item, null);
}
tb.Rows.Add(values);
}
return tb;
}
public static bool IsNullable(Type t)
{
return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
} public static Type GetCoreType(Type t)
{
if (t != null && IsNullable(t))
{
if (!t.IsValueType)
{
return t;
}
else
{
return Nullable.GetUnderlyingType(t);
}
}
else
{
return t;
}
}

最新文章

  1. HDFS --访问
  2. jsp发布:tomcat+花生壳
  3. phpcms安装
  4. whatweb
  5. Websocket 概述
  6. jquery选择器效率优化问题
  7. Mybatis 和 Spring配置
  8. Objective-C关于分类、扮演、协议
  9. Spring学习笔记(二)Spring基础AOP、IOC
  10. jqueryrotate 使用 帮助 笔记 学习
  11. 网络流系列算法总结(bzoj 3438 1061)
  12. 2 _RESETFUL介绍
  13. 基于.NET Core的Hypertext Application Language(HAL)开发库
  14. Centos 6.5 安装python3.6
  15. Arduino 数字函数总结
  16. PHP redis 群发短信
  17. MFC事件和线程
  18. Python 基础知识(一)
  19. c#输出指定信息到文本文件中(追加方式)
  20. #1490 : Tree Restoration-(微软2017在线笔试)

热门文章

  1. 你向 Mysql 数据库插入 100w 条数据用了多久?
  2. Zookeeper请求处理原理分析
  3. Unity移动端入门 - Android那些事
  4. eclipse中maven依赖的jar源码中文注释乱码解决
  5. 转载:【TP5.0】TP5 Validate 验证规则
  6. 本地快速搭建MarkDown语法网站
  7. 小米手机root
  8. HIVE出现Read past end of RLE integer from compressed stream Stream for column 1 kind LENGTH position: 359 length: 359 range: 0错误
  9. java只允许输入数字字母下划线中文
  10. Python之多态案例