【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< ...
随机推荐
- python--boto3 之 与dynamoDB 的基本交互,表的备份与恢复
最近因工作需要,研究了一下boto3中dynamoDB部分,略有心得,在此总结一下. 首先是boto3的安装,在装有python和pip的机器上,运行 sudo pip install boto3 官 ...
- git的问题(error: object file .git/objects/* is empty...)的解决方案及对git版本库文件的了解
由于操作不当,导致git版本库出了大问题,如下所示: error: object file .git/objects/8b/61d0135d3195966b443f6c73fb68466264c68e ...
- axios拦截器请求头携带token
转—— https://github.com/superman66/vue-axios-github/blob/master/src/http.js
- BZOJ4773: 负环(倍增Floyd)
题意 题目链接 Sol 倍增Floyd,妙妙喵 一个很显然的思路(然而我想不到是用\(f[k][i][j]\)表示从\(i\)号点出发,走\(k\)步到\(j\)的最小值 但是这样复杂度是\(O(n^ ...
- java.sql.SQLException: Incorrect string value: '\xF0\x9F\x9A\x80\xF0\x9F...' for column 'name' at row 1
1.异常提示: 12:59:10.000 [http-nio-8080-exec-40] DEBUG o.s.j.s.SQLStateSQLExceptionTranslator - Extracte ...
- Python基础-模块与包
一.如何使用模块 上篇文章已经简单介绍了模块及模块的优点,这里着重整理一下模块的使用细节. 1. import 示例文件:spam.py,文件名spam.py,模块名spam #spam.py pri ...
- Java设计模式—观察者模式
观察者模式(Observer Pattern)也叫做发布订阅模式(Publish/subscribe). 其定义如下: 定义对象间一种一对多的依赖关系,使得每当一个对象改变状态,则所有依赖于它的对象都 ...
- QT 编译遇到重定义;不同的基类型&在QT中使用C++ lib库
最近在使用osg和qt开发,在集成osg时候因为我使用的qt版本为非opengl的版本,导致qt自己封了一遍opengl的一些基类变量如double 这时候就会跟osg中声明的opengl的类型冲突, ...
- WebRequest的get及post提交
static string get_html(string url) { var request = WebRequest.Create(url); var response = request.Ge ...
- POI读取xls和xlsx
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...