DataTable与List的相互转换
List转DataTable:
public static DataTable ToDataTable<T>(IEnumerable<T> collection)
{
var props = typeof(T).GetProperties();
var dt = new DataTable();
foreach (PropertyInfo pi in props)
{
// 当字段类型是Nullable<>时
Type colType = pi.PropertyType; if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[];
}
dt.Columns.Add(new DataColumn(pi.Name, colType));
}
if (collection.Count() > )
{
for (int i = ; i < collection.Count(); i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in props)
{
object obj = pi.GetValue(collection.ElementAt(i), null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
dt.LoadDataRow(array, true);
}
}
return dt;
}
DataTable 转List:
public class ModelConvertHelper<T> where T : new()
{
public static List<T> ConvertToModel(DataTable dt)
{
// 定义集合
List<T> ts = new List<T>(); // 获得此模型的类型
Type type = typeof(T);
string tempName = ""; 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))
{
// 判断此属性是否有Setter
if (!pi.CanWrite) continue; object value = dr[tempName];
if (value != DBNull.Value)
pi.SetValue(t, value, null);
}
}
ts.Add(t);
}
return ts;
}
}
DataTable与List的相互转换的更多相关文章
- DataTable和Json的相互转换
1 #region DataTable 转换为Json字符串实例方法 2 /// <summary> 3 /// GetClassTypeJosn 的摘要说明 4 /// </sum ...
- C# DataTable与实体的相互转换
using System; using System.Collections.Generic; using System.Data; using System.Reflection; namespac ...
- datatable和list的转换
在开发中,把查询结果以DataTable返回很方便,但是在检索数据时又很麻烦,没有list<T>检索方便.但是数据以ILIST形式返回,就为我们在.NET中使用传统的数据绑定造成了不便.下 ...
- Newtonsoft.Json 与 DataTable的相互转换
1.这里下载:http://www.newtonsoft.com/products/json/ 安装: 解压下载文件,得到Newtonsoft.Json.dll 在项目中添加引用 2.引入 ...
- DataTable数据与Excel表格的相互转换
using Excel = Microsoft.Office.Interop.Excel; private static Excel.Application m_xlApp = null; /// & ...
- List<T> 和DataTable的相互转换
我用的将集合类转换为DataTable 的方法 /// <summary> /// 将集合类转换成DataTable /// </summary> /// <param ...
- C#中DataTable与XML格式的相互转换
1.DataTable转换成XML public string ConvertDataTableToXML(DataTable xmlDS) { MemoryStream stream = null; ...
- List,泛型和Datatable 的相互转换
public static DataTable ToDataTableTow(IList list) { DataTable result = new DataTable(); ) { Propert ...
- DataTable / DataSet 与 xml 的相互转换
之前做DataTable和DataSet转xml一直使用XmlSerializer 序列化完成.今天发现新方法,哇咔咔方便了很多.还不用担心Name为空时报错 static void Main(str ...
随机推荐
- String字面量
public class assa{ static String ee = "aa";//ee指向常量池中的aa static String ff = new String(&qu ...
- 序列&权限&索引&视图的语句
create sequence 订单_订单编号_seq -- 创建序列 (成功后在sequence中查询) increment by start with maxvalue nocycle nocac ...
- C++11 使用异步编程std::async和std::future
先说明一点:std::asyanc是std::future的高级封装, 一般我们不会直接使用std::futrue,而是使用对std::future的高级封装std::async. 下面分别说一下. ...
- 手写RPC框架
https://www.bilibili.com/video/av23508597?from=search&seid=6870947260580707913 https://github.co ...
- sx1278 手册参考
记录下芯片的重要数据和内容,方便查阅,无代码实现 参考程序地址:http://www.pudn.com/Download/item/id/3070942.html http://www.cirmal ...
- Python:判断文本中的用户名在数据库中是否存在,存在返回1,不存在返回0
下面是我写的python的一个小脚本,作用是:判断文本中的用户名在数据库中是否存在,存在返回1,不存在返回0.用的是MySQL数据库. 要注意的是:strip函数的使用,该函数的作用是去除字符串两端多 ...
- java中加与不加public
加public表示全局类,该类可以import到任何类内.不加public默认为保留类,只能被同一个包内的其他类引用来源:https://blog.csdn.net/qq_15037231/artic ...
- IdentityServer4授权和认证集成Identity和profile
identiyt的使用可以看之前的文章:https://www.cnblogs.com/nsky/p/10323415.html 之前的ids4授权服务器都是用的in-men方式把数据添加到内存, 现 ...
- 蓝桥杯 基础训练 2n皇后
数月前做的2N皇后基本看书敲代码的,然后发现当时的代码不对,正好做到算法提高的8皇后·改,顺便把以前的代码顺带改了下,题目如下: 问题描述 给定一个n*n的棋盘,棋盘中有一些位置不能放皇后.现在要向棋 ...
- CurrentHashMap、HashMap、HashTable的区别
HashTable 底层数组+链表实现,无论key还是value都不能为null,线程安全,实现线程安全的方式是在修改数据时锁住整个HashTable,效率低,ConcurrentHashMap做了相 ...