【2017001】IList转DataTable、DataTable转IList
IList转DataTable、DataTable转IList
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using System.Reflection; using System.Text; namespace Framework.Utility { public static class DataTableHelper { public static DataTable ConvertTo<T>(IList<T> list) { DataTable table = CreateTable<T>(); Type entityType = typeof(T); PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType); foreach (T item in list) { DataRow row = table.NewRow(); foreach (PropertyDescriptor prop in properties) row[prop.Name] = prop.GetValue(item); table.Rows.Add(row); } return table; } public static IList<T> ConvertTo<T>(IList<DataRow> rows) { IList<T> list = null; if (rows != null) { list = new List<T>(); foreach (DataRow row in rows) { T item = CreateItem<T>(row); list.Add(item); } } return list; } public static IList<T> ConvertTo<T>(DataTable table) { try { if (table == null) return null; List<DataRow> rows = new List<DataRow>(); foreach (DataRow row in table.Rows) rows.Add(row); return ConvertTo<T>(rows); } catch(Exception ex) { string err = ex.ToString(); return null; } } public static T CreateItem<T>(DataRow row) { string columnName; T obj = default(T); if (row != null) { obj = Activator.CreateInstance<T>(); foreach (DataColumn column in row.Table.Columns) { columnName = column.ColumnName; //Get property with same columnName PropertyInfo prop = obj.GetType().GetProperty(columnName); if (prop == null) continue; ) continue; try { //Get value for the column object value = (row[columnName].GetType() == typeof(DBNull)) ? null : row[columnName]; //Set property value if (prop.CanWrite) //判断其是否可写 prop.SetValue(obj, value, null); } catch { throw; } } } return obj; } public static DataTable CreateTable<T>() { Type entityType = typeof(T); DataTable table = new DataTable(entityType.Name); PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType); foreach (PropertyDescriptor prop in properties) table.Columns.Add(prop.Name, prop.PropertyType); return table; } /// <summary> /// datatable 转换成list /// 调用方法:List<Entity> list = DataTableHelper.ConvertToEx<Entity>(dt); /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dt"></param> /// <returns></returns> public static List<T> ConvertToEx<T>(DataTable dt) where T : new() { if (dt == null) return null; ) return null; List<T> list = new List<T>(); Type type = typeof(T); PropertyInfo[] propertyInfos = type.GetProperties(); //获取泛型的属性 List<DataColumn> listColumns = dt.Columns.Cast<DataColumn>().ToList(); //获取数据集的表头,以便于匹配 T t; foreach (DataRow dr in dt.Rows) { t = new T(); foreach (PropertyInfo propertyInfo in propertyInfos) { try { DataColumn dColumn = listColumns.Find(name => name.ToString().ToUpper() == propertyInfo.Name.ToUpper()); //查看是否存在对应的列名 if (dColumn != null) propertyInfo.SetValue(t, dr[propertyInfo.Name], null); //赋值 } catch (Exception ex) { throw new Exception(ex.Message); } } list.Add(t); } return list; } } }
【2017001】IList转DataTable、DataTable转IList的更多相关文章
- DataTable转换成IList<T>的简单实现
DataTable的无奈 很多时候,我们需要去操作DataTable.但DataTable的操作,实在是太不方便了.Linq?lambda表达式?统统没有... 特别是对现有结果集做进一步筛选,这样的 ...
- C# IList<T>转为DataTable
public class WebUtil { /// <summary> /// 转换IList<T>为DataTable/// </summary> /// &l ...
- C# 中 DataTable转换成IList
在用C#作开发的时候经常要把DataTable转换成IList:操作DataTable比较麻烦,把DataTable转换成IList,以对象实体作为IList的元素,操作起来就非常方便. 注意:实体的 ...
- DataTable转换成IList 【转载】
链接:http://www.cnblogs.com/hlxs/archive/2011/05/09/2087976.html#2738813 留着学习 using System; using Syst ...
- DataTable转换成IList
//文章出处: http://www.cnblogs.com/hlxs/archive/2011/05/09/2087976.html DataTable转换成IList 在用C#作开发的时候经常要把 ...
- 将SqlDataReader 数据集转化为datatbale ,在将datatable 转化为iList
public IList GetModelList(string tablename, string where) { IList list = null; DataTable dataTable = ...
- [DataTable] datatable根据表中的字段进行排序
private DataTable SortTable(DataTable dt,string[] pids) { DataTable dt0 = dt.Clone(); //复制原表结构 ;i< ...
- MVC中一般为什么用IQueryable而不是用IList?用IQueryable比IList好在哪?
IList(IList<T>)会立即在内存里创建持久数据,这就没有实现"延期执行(deferred execution)",如果被加载的实体有关联实体(associat ...
- 完整DataTable与IList互换(转)
public class CollectionHelper { private CollectionHelper() { } public static DataTable ConvertTo< ...
随机推荐
- 打印thinkphp中的sql语句
var_dump($repair->fetchSql(true)->where(array('cuername' =>$cuername))->order('applytime ...
- js小数转分数-近似递归
精度为小数两位,提高精度可把 toFixed(2)和100同时修改: function decimalsToFractional(decimals){ const formatDecimals = d ...
- Microsoft Windows Scripting Self-Paced Learning Guide
http://www.mums.ac.ir/shares/hit/eduhit/book/windowsscripting.pdfhttp://support.microsoft.com/kb/926 ...
- postgres备份数据库
1. psql --help psql is the PostgreSQL interactive terminal. Usage: psql [OPTION]... [DBNAME [USERNAM ...
- Raft协议--中文论文介绍
本篇博客为著名的 RAFT 一致性算法论文的中文翻译,论文名为<In search of an Understandable Consensus Algorithm (Extended Vers ...
- python 多进程数据交互及共享
多线程和多进程最大的不同在于,多进程中,同一个变量,各自有一份拷贝存在于每个进程中,互不影响,而多线程中,所有变量都由所有线程共享,所以,任何一个变量都可以被任何一个线程修改,因此,线程之间共享数据最 ...
- C#获取apk版本信息
获取很多人都会问我为什么要写这个博客,原因很简单,这次研发apk版本信息的时候网上查了很多的资料都没有这方面的信息,因此这次功能完了想写下方法,如果以后博友们遇到了可以直接copy,不用花很多的时间, ...
- mongodb时间点备份恢复
1:创建测试数据 > use maxiangqian switched to db maxiangqian "}) WriteResult({ "nInserted" ...
- sql server——分组查询(方法和思想)
思想 先排序在汇总 sql server里分组查询通常用于配合聚合函数,达到分类汇总统计的信息.而其分类汇总的本质实际上就是先将信息排序,排序后相同类别的信息会聚在一起,然后通过需求进行统计计算. 使 ...
- CPP11实践
- 001 以lambda作为返回值该如何声明? 标准库提供了function模板可以直接声明,如std::function<void (int, int)>.如下函数foo返回了一个函数 ...