C# DataTable转List And List转DataTable
// DataTable转List: IList<HousesEntity> Ilist = TableAndList.ConvertTo<HousesEntity>(dt);
public static class TableAndList
{ public static DataTable ConvertTo<T>(IList<T> list)
{
DataTable table = CreateTable<T>();
Type entityType = typeof(T);
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);
foreach (T item in list)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item);
table.Rows.Add(row);
}
return table;
}
public static IList<T> ConvertTo<T>(IList<DataRow> rows)
{
IList<T> list = null;
if (rows != null)
{
list = new List<T>();
foreach (DataRow row in rows)
{
T item = CreateItem<T>(row);
list.Add(item);
}
}
return list;
}
/// <summary>
/// DataTable To List
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="table"></param>
/// <returns></returns>
public static IList<T> ConvertTo<T>(DataTable table)
{
if (table == null)
return null; List<DataRow> rows = new List<DataRow>();
foreach (DataRow row in table.Rows)
rows.Add(row); return ConvertTo<T>(rows);
}
public static T CreateItem<T>(DataRow row)
{
string columnName;
T obj = default(T);
if (row != null)
{
obj = Activator.CreateInstance<T>();
foreach (DataColumn column in row.Table.Columns)
{
columnName = column.ColumnName;
//第一行如果是Rows 则跳过
if (columnName=="Rows")
{
continue;
} //Get property with same columnName
PropertyInfo prop = obj.GetType().GetProperty(columnName);
try
{
//Get value for the column
object value = (row[columnName].GetType() == typeof(DBNull))
? null : row[columnName];
//Set property value
if (prop.CanWrite) //判断其是否可写
prop.SetValue(obj, value, null);
}
catch
{
throw;
//Catch whatever here
}
// }
}
}
return obj;
}
public static DataTable CreateTable<T>()
{
Type entityType = typeof(T);
DataTable table = new DataTable(entityType.Name);
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType); foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, prop.PropertyType);
return table;
} }
C# DataTable转List And List转DataTable的更多相关文章
- “DataTable”是“System.Data.DataTable”和“Microsoft.Office.Interop.Excel.DataTable”之间的不明确的引用
“DataTable”是“System.Data.DataTable”和“Microsoft.Office.Interop.Excel.DataTable”之间的不明确的引用 造成这个错误的原因是,在 ...
- 多个不同的表合并到一个datatable中,repeater在绑定datatable
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- 将两个列不同的DataTable合并成一个新的DataTable
/// <summary> /// 将两个列不同(结构不同)的DataTable合并成一个新的DataTable /// </summary> ...
- C#之DataTable转List与List转Datatable
闲来无事,只有写代码啦,以下为DataTable转List与List转DataTable的两个方法,主要技术点用到了反射原理: /// <summary> /// 模型转换类 /// &l ...
- C#给DataTable添加序号、C#给DataTable添加合计、小计
/// <summary> /// 给DataTable添加序号 /// </summary> /// <param name= ...
- Datatable的查找和排序(Datatable.Select)
Datatable 是一种常用的数据结构.数据类型有点类似于数据库中的表结构.在没有使用优秀的orm框架前,大部分的数据库的数据都是先变为Datatable 然后再通过代码转换变成 object. ...
- EasyUI - Datatable转Json and Json转Datatable
using System; using System.Data; using System.Linq; using System.Collections; using System.Collectio ...
- C# 将DataTable一行放入另一个DataTable中
http://blog.csdn.net/huyu107/article/details/53509171 概述 从一个DataTable中取一行放到另一个DataTable里报错: 该行已经属于另一 ...
- NPOI json转Excel DataTable转Excel ,Excel转DataTable
JsonToExcel: public static void JsonToExcel(List<Dictionary<string, object>> json, strin ...
随机推荐
- 【转】Optiplex 7010驱动下载链接(XP&Windows7
原文网址:http://zh.community.dell.com/support_forums/desktops/f/236/t/2606 x 7010驱动下载链接(XP&Windows7) ...
- ES5新特性:理解 Array 中增强的 9 个 API
为了更方便的对JS中Array进行操作,ES5规范在Array的原型上新增了9个方法,分别是forEach.filter.map.reduce.reduceRight.some.every.index ...
- 速冻熟食制品的QS的申请办法
QS审核流程许可证申办程序食品生产加工企业按照下列程序申请获得食品生产许可证:1.食品生产加工企业按照地域管辖和分级管理的原则,到所在地的市(地)级以上质量技术监督部门提出办理食品生产许可证的申请:2 ...
- 用Oracle的TRIM函数去除字符串首尾指定字符
去掉首尾空格 SELECT TRIM(' abc '), ltrim(' abc '), rtrim(' abc ') FROM dual; 去掉首尾的其他字符 SELECT /*TRIM(';a;b ...
- GUN/GPL 中文译文
版权所有 (C) 1989,1991 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307U ...
- libcurl断点下载
开发需要写了一个处理curl 下载的c++类,内含有多个静态方法和实例方法,写在这里留给有需求的同学 头文件 CURLHelper.h enum CURLHelperStateType { CURLH ...
- 【CSS3】Advanced6:Attribute Selectors
1.with the attribute abbr[title]{color:red} 2.with the attribute and it's value input[type=text][dis ...
- synchronize学习
这个例子我们看到,java中将对象或者Class对象当做锁 package synchronized简单使用; public class Test7 extends Thread{ public st ...
- vijosP1210 盒子与球
vijosP1210 盒子与球 链接:https://vijos.org/p/1210 [思路] Stirling+全排列. 因为第二类stirling所求是没有标明盒子顺序的方案数,所以最后需要乘一 ...
- office文件密码破解方法及软件
今天会用到3个软件 1.Office Password Remover 说明:这个软件可以很快破解.doc .xls的密码 使用方法:参考百度经验里面的文章http://jingyan.baidu ...