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 ...
随机推荐
- oracle得到拼音函数
CREATE OR REPLACE FUNCTION fgetpy (v_str VARCHAR2) RETURN VARCHAR2AS v_strlen INT; v_return ...
- iOS的属性声明:retain和strong的区别
声明属性时用strong或者retain效果是一样的(貌似更多开发者更倾向于用strong).不过在声明Block时,使用strong和retain会有截然不同的效果.strong会等于copy,而r ...
- 滚动到指定元素的id处+当元素出现在浏览器显示区域就会自动加载
//滚动到指定元素的id处 如:$("#Exam82") function Jump() { var scroll_offset = $("#Exam82"). ...
- 启动两个tomcat
因为项目的种种原因,必须启动两个tomcat测试 于是复制tomcat,改端口,报错,到日志看,发现shutdow端口也需要改 总结 server.xml改两个地方的端口 <Server por ...
- dobbo学习
http://www.cnblogs.com/Javame/p/3632473.html 1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以 ...
- 鼠标悬停移除更换class
$("#xinl").mouseover(function() //鼠标悬停执行函数 { $(".xl").removeClass().addClass(&q ...
- Max Min
def main(): n = int(raw_input()) k = int(raw_input()) k_arr = [] min_dif = 9999999999 # 根据input要求,规定 ...
- Android开源框架之SwipeListView导入及模拟QQ侧滑
SwipeListView是Github上的一个开源框架,地址:https://github.com/47deg/android-swipelistview SwipeListView was bor ...
- Linux - create usergroup, user and Assigning permissions
第一步:登录已有的Linux系统,使用root账户,登录好以后,如下图: 这样,就登录到Linux系统中,而且是用root用户登录的 注意:如果,你想要创建用户和用户组,那么你当前登录的用户必须有ro ...
- cf A. Jeff and Digits
http://codeforces.com/contest/352/problem/A #include <cstdio> #include <cstring> #includ ...