public class DataConvert
{
public static List<T> ConvertDataTable<T>(DataTable dt)
{
List<T> data = new List<T>();
foreach (DataRow row in dt.Rows)
{
T item = GetItem<T>(row);
data.Add(item);
}
return data;
}
public static T GetItem<T>(DataRow dr)
{
Type temp = typeof(T);
T obj = Activator.CreateInstance<T>(); var properties = temp.GetProperties(); for(int i=;i<properties.Length;i++)
{
var prop = properties[i];
var itemValue = dr.ItemArray[i];
if(itemValue!=DBNull.Value)
{
prop.SetValue(obj, itemValue, null);
}
} //foreach (DataColumn column in dr.Table.Columns)
//{
// foreach (PropertyInfo pro in temp.GetProperties())
// {
// if (pro.Name == column.ColumnName)
// pro.SetValue(obj, dr[column.ColumnName], null);
// else
// continue;
// }
//}
return obj;
}
}

convert datatable to List<T>的更多相关文章

  1. Convert DataTable to List<T> where Class of List is Dynamic

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...

  2. Asp.net C# 把 Datatable转换成JSON 字符串

    First of all, we have to fetch the records from the database (MS Sqlserver) into the C# DataTable, o ...

  3. csharp:asp.net Importing or Exporting Data from Worksheets using aspose cell

    using System; using System.Data; using System.Configuration; using System.Collections; using System. ...

  4. DataTableToList

    很简单的转换功能,这是我在GitHub上复制的一段代码(懒得再去找原地址了),感觉功能还算可以,贴出来分享给大家 /// <summary> /// DataTable to List c ...

  5. C#动态设置匿名类型对象的属性

    用C#写WPF程序, 实现功能的过程中碰到一个需求: 动态设置对象的属性,属性名称是未知的,在运行时才能确定. 本来这种需求可以用 Dictionary<string, object> 实 ...

  6. C# 数据操作系列 - 4. 自己实现一个ORM

    0. 前言 在之前的几篇内容中,我们了解了如何通过ADO.NET 访问数据库,如何修改.新增数据.如何通过DataSet和DataAdapter获取数据,我们将在这一篇试试自己实现一个简单的ORM框架 ...

  7. C# convert json to datatable,convert list to datatable

    static DataTable ConvertJsonToTable(string jsonValue) { DataTable dt = (DataTable)JsonConvert.Deseri ...

  8. DataTable转换成IList<T>的简单实现

    DataTable的无奈 很多时候,我们需要去操作DataTable.但DataTable的操作,实在是太不方便了.Linq?lambda表达式?统统没有... 特别是对现有结果集做进一步筛选,这样的 ...

  9. DataTable与DTO对象的简易转换类

    在web开发过程中,有时候为了数据传输的方便,比如:后台需要更新前端的ViewModel,此时我们定义一个与前端ViewModel结构一样的DTO对象,从数据层获取数据后,将数据封装成DTO然后序列化 ...

随机推荐

  1. wpf/winform获取windows10系统颜色和主题色

    Windows10开始微软在系统颜色中添加了深色,对于UWP来说很轻松就能获取到系统当前的颜色和主题色,而对于Win32应用就没有那么直观了. 在wpf中,可以通过SystemParameters.W ...

  2. .net ajax跨域请求问题

    </system.codedom>     <system.webServer>         <defaultDocument>             < ...

  3. SpringBoot 教程之属性加载详解

    免费Java高级资料需要自己领取,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G.            ...

  4. PHP开发人员对JAVA的WEB开发入门(初版-基础知识)

    最近准备对其他部门PHP开发的童鞋做一个对JAVA的培训.知己知彼,百战不殆,我要先了解点PHP,才能确认他们的基础,达到好的授课效果. PHP(原始为Personal Home Page的缩写,后正 ...

  5. log4cxx日志库在Windows+VS2017上的编译使用

    项目中用到了log4cxx,但是Debug版本运行时老是提示找不到Properities::setProperty?怀疑是提供的库有问题,所以尝试源码来重新编译一下.log4cxx官方主页:https ...

  6. mssql 单引号插入到sqlserver的方法分享

    转自:http://www.maomao365.com/?p=6740 摘要: 下文讲述sqlserver操作中遇到单引号的处理方法sqlserver 数据库中可以存储任何字符,因为在数据库中字符都是 ...

  7. Django—常见问题解决

    让Django项目运行允许外部访问 1.将python manage.py runserver 改为 python manage.py runserver 0.0.0.0:80 或者 python m ...

  8. apicloud含有微信支付。支付宝支付和苹果内购的代码

    apicloud含有微信支付.支付宝支付和苹果内购的代码 <!DOCTYPE html> <html> <head> <meta charset=" ...

  9. c# 第30节 类字段与属性

    本节内容: 1:字段是什么 2:属性判断字段的安全 3:对属性的解释 1:字段是什么 字段其实在上一节我们就使用过了:再来详细的说他一说 amespace cw { public enum gende ...

  10. 鲜贝7.3--navicat破解

    Navicat for MySQL 安装软件和破解补丁: 链接:https://pan.baidu.com/s/1yy5JkrXk5NV129wzkzntqw 提取码:htc2 1.安装Navicat ...