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 ...
随机推荐
- elk-插件(head、X-pack)(五)
一.修改ES配置,允许REST跨源操作ES服务器,添加以下2个配置,并重启ES. http.cors.enabled: true #如果启用了 HTTP 端口,那么此属性会指定是否允许跨源 REST ...
- ubuntu安装notepad++
sudo add-apt-repository ppa:notepadqq-team/notepadqq sudo apt-get update sudo apt-get install notepa ...
- Win7-IE11 For x86&x64离线安装包
一.Internet Explorer11简体中文版离线安装包: 微软已停止了IE11以下版本(包括IE10/9/8)的技术支持.以后Win7用IE11的机会也越来越多,但IE11官方安装 ...
- drawrect&layoutsubviews
drawrect触发方法: 设置frame setneeddisplay contentmode设置为redraw sizetofit layoutsubviews触发方法 setframe layo ...
- git常用操作命令使用说明
设置用户名和邮箱 git config --global user.email 'xxx' git config --global user.name 'xxx' 创建分支 git branch xx ...
- 20190412 T-SQL语言二
Use xsxk;WITH c_count(id,xb,rs)AS (SELECT 班级,性别,count(*)FROM XS GROUP BY 班级,性别 ) SELECT * FROM c_cou ...
- Linux+DDoS deflate 预防DDoS攻击
使用DDoS脚本防止DDoS攻击 使用DDoS脚本防止DDoS攻击: DDoS概述: 分布式拒绝服务(DDoS:Distributed Denial of Service)攻击,指借助于客户/服务 ...
- 发布WebApi项目时包含XML文档文件
Open your publishprofile (*.pubxml) and include this code into "Project" element: <Item ...
- SQL Prompt snippets
SQL Prompt snippets https://github.com/gvohra/sqlpromptsnippets
- Jmeter登录后Session自动共享与多线程组并行
在接口测试中,出于安全考虑接口是需要session才能访问.另外在此基础上,我们还可能模拟不同的客户端登录,需要并行运行移动端线程组. 实现session共享1)修改jmeter安装目录bin下的jm ...