ListHelper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
namespace Data_Helper
{
/// <summary>
/// List 操作(dataTable、dataSet 等之间的转换)
/// </summary>
public class ListHelper
{
/// <summary>
/// 将 dataTable 转换成 List
/// </summary>
/// <typeparam name="T">要转换成的对象</typeparam>
/// <param name="dataTable">要转换的DataTable</param>
/// <returns></returns>
public static List<T> ConvertToList<T>(DataTable dataTable)
{
T obj = default(T);
List<T> objList = new List<T>();
PropertyInfo[] objProperties = null;
string propertieName = null;
try
{
foreach (DataRow row in dataTable.Rows)
{
obj = Activator.CreateInstance<T>();//动态创建类型实例,不需要引用类库
objProperties = obj.GetType().GetProperties();
foreach (PropertyInfo properties in objProperties)
{
if (properties != null)
propertieName = properties.Name;
if (propertieName != null && dataTable.Columns.Contains(propertieName))
{
object value = row[propertieName];
if (value.GetType() == typeof(System.DBNull))
value = null;
properties.SetValue(obj, value, null);
}
}
objList.Add(obj);
}
}
catch (Exception ex)
{
throw ex;
}
return objList;
}
/// <summary>
/// 将 List 转换成 dataTable
/// </summary>
/// <typeparam name="T">要转换成的对象</typeparam>
/// <param name="objList">要转换的List</param>
/// <returns></returns>
public static DataTable ConvertToDataTable<T>(List<T> objList)
{
if (objList == null || objList.Count <= 0)
return null;
DataTable dt = new DataTable(typeof(T).Name);
PropertyInfo[] objProperties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
try
{
DataColumn column;
DataRow row;
PropertyInfo property;
string propertyName = null;
foreach (T obj in objList)
{
if (obj == null)
continue;
row = dt.NewRow();
for (int i = 0, j = objProperties.Length; i < j; i++)
{
property = objProperties[i];
propertyName = property.Name;
if (propertyName != null && dt.Columns[propertyName] == null)
{
column = new DataColumn(propertyName, property.PropertyType);
dt.Columns.Add(column);
}
row[propertyName] = property.GetValue(obj, null);
}
dt.Rows.Add(row);
}
}
catch (Exception ex)
{
throw ex;
}
return dt;
}
/// <summary>
/// 将 List 转换成 dataSet
/// </summary>
/// <typeparam name="T">要转换成的对象</typeparam>
/// <param name="objList">要转换的objList</param>
/// <returns></returns>
public static DataSet ConvertToDataSet<T>(List<T> objList)
{
if (objList == null || objList.Count <= 0)
return null;
DataSet ds = new DataSet();
try
{
DataTable dt = ConvertToDataTable(objList);
ds.Tables.Add(dt);
}
catch (Exception ex)
{
throw ex;
}
return ds;
}
}
}
ListHelper的更多相关文章
- 剑指Offer面试题:15.反转链表
一.题目:反转链表 题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的头结点. 链表结点定义如下,这里使用的是C#描述: public class Node { public in ...
- JAVA 多线程编程之一(基础)
1.原子变量(java.util.concurrent.atomic) 原子状态,变化不会被打断,如 AtomicLong , AtomicInteger 2.内部锁 synchronized 块 ...
- 【niubi-job——一个分布式的任务调度框架】----框架设计原理以及实现
引言 niubi-job的框架设计是非常简单实用的一套设计,去掉了很多其它调度框架中,锦上添花但并非必须的组件,例如MQ消息通讯组件(kafka等).它的框架设计核心思想是,让每一个jar包可以相对之 ...
- Java并发编程笔记—基础知识—实用案例
如何正确停止一个线程 1)共享变量的使用 中断线程最好的,最受推荐的方式是,使用共享变量(shared variable)发出信号,告诉线程必须停止正在运行的任务.线程必须周期性的核查这一变量(尤其在 ...
- A Star算法笔记
回顾A*算法,偶得一源代码,略有瑕疵,改正之,并置于下. using System; using System.Collections.Generic; using System.Linq; usin ...
- ASP.NET SignalR2持久连接层解析
越是到年底越是感觉浑身无力,看着啥也不想动,只期盼着年终奖的到来以此来给自己打一针强心剂.估摸着大多数人都跟我一样犯着这样浑身无力的病,感觉今年算是没挣到啥钱,但是话也不能这么说,搞得好像去年挣到钱了 ...
- Java 并发编程(三)为线程安全类中加入新的原子操作
Java 类库中包括很多实用的"基础模块"类.通常,我们应该优先选择重用这些现有的类而不是创建新的类.:重用能减少开发工作量.开发风险(由于现有类都已经通过測试)以及维护成本.有时 ...
- C# List 扩展排序
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Comm ...
- Java并发编程实战(chapter_2)(对象发布、不变性、设计线程安全类)
一.发布与溢出 "发布(Publish)"一个对象的意思是指,使对象能够在当前作用于之外的代码中使用.这个"之外",尤为关键,各种出问题的地方,都是因为这个&q ...
随机推荐
- SQL Server 2005中的分区表(三):将普通表转换成分区表
在设计数据库时,经常没有考虑到表分区的问题,往往在数据表承重的负担越来越重时,才会考虑到分区方式,这时,就涉及到如何将普通表转换成分区表的问题了. 那么,如何将一个普通表转换成一个分区表 呢?说到底, ...
- 读取并解析properties文件
public class SysConfig { private static final Properties properties = new Properties(); static{ Reso ...
- HDU 5904 - LCIS (BestCoder Round #87)
HDU 5904 - LCIS [ DP ] BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式 ...
- BestCoder Round #36 (hdu5200)Strange Class(离线)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Trees Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 4810 这道题 是属于什么类型?
统计每一位出现1的个数 求组合数 直接贴代码 #include <iostream> #include <cstdio> #include <cmath> #in ...
- xml解析,练习
<collection shelf="New Arrivals"><movie title="Enemy Behind"> < ...
- Ubuntu 修改 Apache2 用户组 方法
检查/etc/apache2/envvars文件,发现其中需要使用/etc/apache2/envvars文件中的以下几个环境变量 export APACHE_RUN_USER=www-data ex ...
- hdu 5305Friends
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5305 Problem Description There are n people and m pai ...
- layout cannot be resolved or is not a field
去除代码activity代码页面顶部中的 import android.R;这句就可以消除红色波浪线的main cannot be resolved or is not a field类似这个错误了
- Codeforces 414B Mashmokh and ACM
http://codeforces.com/problemset/problem/414/B 题目大意: 题意:一个序列B1,B2...Bl如果是好的,必须满足Bi | Bi + 1(a | b 代表 ...