在将DataTable转换为List<T>时,找到了网上的方案,原文链接:http://stackoverflow.com/questions/4593663/fetch-datarow-to-c-sharp-object

使用时,遇到DbNull无法正常转换的问题,所以做了修正补充,继续发代码上来。

欢迎补充修正。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Reflection; public static class DataTableExtensions
{
public static IList<T> ToList<T>(this DataTable table) where T : new()
{
IList<PropertyInfo> properties = typeof(T).GetProperties().ToList();
IList<T> result = new List<T>(); foreach (var row in table.Rows)
{
var item = CreateItemFromRow<T>((DataRow)row, properties);
result.Add(item);
} return result;
} public static IList<T> ToList<T>(this DataTable table, Dictionary<string, string> mappings) where T : new()
{
IList<PropertyInfo> properties = typeof(T).GetProperties().ToList();
IList<T> result = new List<T>(); foreach (var row in table.Rows)
{
var item = CreateItemFromRow<T>((DataRow)row, properties, mappings);
result.Add(item);
} return result;
} private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
{
T item = new T();
foreach (var property in properties)
{
property.SetValue(item, row[property.Name], null);
}
return item;
} private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties, Dictionary<string, string> mappings) where T : new()
{
T item = new T();
foreach (var property in properties)
{
if (mappings.ContainsKey(property.Name))
property.SetValue(item, (row[mappings[property.Name]] == DBNull.Value) ? null : row[mappings[property.Name]], null);
}
return item;
}
}

使用扩展方法将DataTable转换为List<T>的更多相关文章

  1. Silverlight中如何自己写方法将DataTable转换为PagedCollectionView数据(动态创建类)

    将DataTable转换为PagedCollectionView数据,我们可以借用DataTable的GetBindableData()方法,如下: DataTable dt=new DataTabl ...

  2. 扩展方法IEnumerable<T>转换为IList<SelectListItem> ,提供@Html.DropDownList使用

    由于在MVC中经常会使用到@Html.DropDownList方法,而该方法接收的是List<SelectListItem> 参数,因此就想着写一个扩展方法,直接把IEnumerable转 ...

  3. .net 扩展方法,lamada表达式 委托

    扩展方法 (1)扩展方法是一种特殊的静态方法,它定义在一个静态类中,但可以在其他类的对象上向调用实例方法那样进行调用.因此,通过扩展方法,我们就可以在不修改一个类型的前提下对一个类型进行功能上的扩充, ...

  4. 【转】给DataTable和DataRow扩展方法,直接转换为对象集合或对象

    /// <summary> /// 类 说 明:给DataTable和DataRow扩展方法,直接转换为对象集合或对象 /// 补充说明:此扩展类可以极大的简化操作,但是性能低下,大数据以 ...

  5. DataTable和DataRow利用反射直接转换为Model对象的扩展方法类

    DataTable和DataRow利用反射直接转换为Model对象的扩展方法类   /// <summary> /// 类 说 明:给DataTable和DataRow扩展方法,直接转换为 ...

  6. C#中DataTable与实体集合通用转换(使用扩展方法)

    本案例提供了:把DataRow转换为单个实体.dataTable转换为List泛型支持时间格式转换. 下文的方法都是扩展方法.扩展方法要求写在静态类中,方法也要静态. 它必须在一个非嵌套.非泛型的静态 ...

  7. DataTable扩展方法ToList<T>()、ToJSON()、ToArrayList()

    /// <summary> /// 扩展方法类 /// </summary> public static class CommonExtension { /// <sum ...

  8. C# DataTable扩展方法

    在日常搬砖中,总结了一些简单的扩展方法. public static bool IsNullOrEmpty(this DataTable dt) { ; } public static bool Is ...

  9. C#扩展方法 DataTable.ToEntitys

    类A需要添加功能,我们想到的就是在类A中添加公共方法,这个显而易见肯定可以,但是由于某种原因,你不能修改类A本身的代码,但是确实又需要增加功能到类A中去,怎么办? 这个时候扩展方法(Extension ...

随机推荐

  1. iOS系统tabbar图标出现重影问题

    大家在自定义tabbar的时候会将系统的tabbar干掉,然后放上自已自定义的tabbar(含有想要的Button)对不对,具体代码如下: /** * 添加自定义的tabBar */ -(void)a ...

  2. IOS百度地图获取所在的城市名称

    笔者的app要实现定位所在省和城市名称,借此总结巩固一下! @interface VenueListVC : BasePageTableViewVC<BMKLocationServiceDele ...

  3. # mysql -u root -p -bash: mysql: command not found

    [root@jboss ~]# mysql -u root -p-bash: mysql: command not found 需要安装mysql # yum install mysql之后就行 了

  4. 本地测试AJAX请求

    要在本地测试AJAX,首先是环境的搭建,因为XHR对象的open方法中参数url是指文件在服务器上的文件.下面以WampServer为例. 1. 下载wamp的安装包,下载地址为:http://221 ...

  5. ray与triangle/quad求交二三事

    引擎中,ray与quad求交,算法未细看,但有求解二次方程,不解.ray与triangle求交,使用的是97年经典算法,仔细看过论文,多谢小武同学指点,用到了克拉默法则求解线性方程组.想模仿该方法,做 ...

  6. iOS内置音频

    Predefined soundsThere are some predefined system sounds, for the system sound ID in the range 1000 ...

  7. [转] Jenkins实战演练之Windows系统节点管理

    [前提] 通过<Jenkins实战演练之Windows服务器快速搭建>(http://my.oschina.net/iware/blog /191818)和<Jenkins实战演练之 ...

  8. LESS用法·

    CSS 彻底改变了 Web 页面的设计,但 CSS 仍然是静态的,而且在其句法发展方面受到限制.这些限制是有目的且合乎情理的,鼓励广泛加以实现.但开发人员和设计人员常常发现 CSS 使用起来很单调乏味 ...

  9. How to do code coverage test for windows service

    First, instrument the exe or dll by command vsinstr -coverage the dll/exe second, start the performa ...

  10. ArcGIS图层介绍

    什么是图层 图层是用来在 ArcGIS 产品套件中显示地理数据集的机制.每个图层代表一种数据集(可以是地图服务.图形或是矢量数据),并指定该数据集是如何描绘使用一组属性的. 包含一个地图控件的每个应用 ...