List<T> 和DataTable的相互转换
我用的将集合类转换为DataTable 的方法
/// <summary>
/// 将集合类转换成DataTable
/// </summary>
/// <param name="list">集合</param>
/// <returns></returns>
private static DataTable ToDataTableTow(IList list)
{
DataTable result = new DataTable();
if (list.Count > )
{
PropertyInfo[] propertys = list[].GetType().GetProperties(); foreach (PropertyInfo pi in propertys)
{
result.Columns.Add(pi.Name, pi.PropertyType);
}
foreach (object t in list)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
object obj = pi.GetValue(t, null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
result.LoadDataRow(array, true);
}
}
return result;
}
将DataTable转换为List<T>
/// <summary>
/// DataTable 转换为List 集合
/// </summary>
/// <typeparam name="TResult">类型</typeparam>
/// <param name="dt">DataTable</param>
/// <returns></returns>
public static List<T> ToList<T>(this DataTable dt) where T : class, new()
{
//创建一个属性的列表
List<PropertyInfo> prlist = new List<PropertyInfo>();
//获取TResult的类型实例 反射的入口 Type t = typeof(T); //获得TResult 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表
Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -) prlist.Add(p); }); //创建返回的集合 List<T> oblist = new List<T>(); foreach (DataRow row in dt.Rows)
{
//创建TResult的实例
T ob = new T();
//找到对应的数据 并赋值
prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null); });
//放入到返回的集合中.
oblist.Add(ob);
}
return oblist;
}
其它的实现方式
/// <summary>
/// 转化一个DataTable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static DataTable ToDataTable<T>(this IEnumerable<T> list)
{
//创建属性的集合
List<PropertyInfo> pList = new List<PropertyInfo>();
//获得反射的入口
Type type = typeof(T);
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 list)
{
//创建一个DataRow实例
DataRow row = dt.NewRow();
//给row 赋值
pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
//加入到DataTable
dt.Rows.Add(row);
}
return dt;
}
返回指定的列
/// <summary>
/// 将泛型集合类转换成DataTable /// </summary>
/// <typeparam name="T">集合项类型</typeparam> /// <param name="list">集合</param>
/// <returns>数据集(表)</returns>
public static DataTable ToDataTable<T>(IList<T> list)
{
return 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 > )
{
PropertyInfo[] propertys = list[].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == )
{
result.Columns.Add(pi.Name, pi.PropertyType);
}
else
{
if (propertyNameList.Contains(pi.Name))
result.Columns.Add(pi.Name, pi.PropertyType);
}
} for (int i = ; i < list.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == )
{
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;
}
}
List<T> 和DataTable的相互转换的更多相关文章
- Newtonsoft.Json 与 DataTable的相互转换
1.这里下载:http://www.newtonsoft.com/products/json/ 安装: 解压下载文件,得到Newtonsoft.Json.dll 在项目中添加引用 2.引入 ...
- List,泛型和Datatable 的相互转换
public static DataTable ToDataTableTow(IList list) { DataTable result = new DataTable(); ) { Propert ...
- C# List和DataTable的相互转换
1.List转DataTable /// <summary> /// list to datatable /// </summary> /// <typeparam na ...
- CollatingOfData 之 JsonHelper
1 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System. ...
- DataTable数据与Excel表格的相互转换
using Excel = Microsoft.Office.Interop.Excel; private static Excel.Application m_xlApp = null; /// & ...
- C#中DataTable与XML格式的相互转换
1.DataTable转换成XML public string ConvertDataTableToXML(DataTable xmlDS) { MemoryStream stream = null; ...
- DataTable与List的相互转换
List转DataTable: public static DataTable ToDataTable<T>(IEnumerable<T> collection) { var ...
- DataTable和Json的相互转换
1 #region DataTable 转换为Json字符串实例方法 2 /// <summary> 3 /// GetClassTypeJosn 的摘要说明 4 /// </sum ...
- DataTable / DataSet 与 xml 的相互转换
之前做DataTable和DataSet转xml一直使用XmlSerializer 序列化完成.今天发现新方法,哇咔咔方便了很多.还不用担心Name为空时报错 static void Main(str ...
随机推荐
- codevs 2495 水叮当的舞步IDA*
/* 比较简单的A* 估价函数很简单就是除了左上角的联通快之外的不同的个数 加上迭代 好像答案最多在16步之内出解 这样裸裸的交上去是50分 在考虑剪枝 每个选颜色的时候一定是选左上角联通快附近的颜色 ...
- 国内优秀npm镜像
淘宝npm镜像 淘宝npm镜像:http://npm.taobao.org/ 1.临时使用 npm --registry https://registry.npm.taobao.org install ...
- Effective java-泛型思维导图
- java编程思想-泛型思维导图
- VS2008试用版到期解决办法
在Windows 7下,VS2008试用版无法正常升级到正式版.原因是维护页面的注册码输入框和升级按钮被隐藏.通过本补丁,可让注册码输入框和升级按钮恢复正常显示 点击此处下载补丁 1.安装完90天试用 ...
- DataTable去重复方法
//去掉重复行 DataTable table=new DataTable(); DataView dv = table.DefaultView; table = dv.ToTable(true, n ...
- ueditor asp.net版本更改图片保存路径
目的:把本地上传的图片放置到跟目录下的Images/Upload文件夹下. 修改步骤: 1.ueditor.config.js文件中的, imagePath: URL + "net/&quo ...
- 初识Activity
Callback Description onCreate() This is the first callback and called when the activity is first cre ...
- SQL批量信息保存(XML格式字符串数据)
/* *功能:SQL批量信息录入 *此存储过程获取表单信息,插入表中.*/CREATE PROC [dbo].[sp_SaveToMX1]@XML text --明细表XML字符串信息ASBEG ...
- 抓取锁的sql语句-第六次修改
增加异常处理 CREATE OR REPLACE PROCEDURE SOLVE_LOCK AS V_SQL VARCHAR2(3000); --定义 v_sql 接受抓取锁的sql语句V_SQL02 ...