使用扩展方法将DataTable转换为List<T>

在将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;
    }
}
时间: 2024-07-30 13:50:08

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

扩展方法IEnumerable&lt;T&gt;转换为IList&lt;SelectListItem&gt; ,提供@Html.DropDownList使用

由于在MVC中经常会使用到@Html.DropDownList方法,而该方法接收的是List<SelectListItem> 参数,因此就想着写一个扩展方法,直接把IEnumerable转换为List<SelectListItem>类型,这样使用起来会比较方便 正式进入正文. 1.首先创建下面实体: //水果类 public class Fruit { public string Code { get; set; } public string Name { get; set; }

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

扩展方法 (1)扩展方法是一种特殊的静态方法,它定义在一个静态类中,但可以在其他类的对象上向调用实例方法那样进行调用.因此,通过扩展方法,我们就可以在不修改一个类型的前提下对一个类型进行功能上的扩充,这种方法并不会产生新的类型,而是采用向已有类中加入新方法的方式来完成功能的扩展. (2)在对已有类进行扩展时,我们需要将所有的扩展方法都写在一个静态类中,这个静态类就相当于存放扩展方法的容器,所有的扩展方法都可以写在这里面.扩展方法与普通方法的声明方式不同,扩展方法的第一个参数以this关键字开始,

c# 之DataTable的扩展方法

由于太懒了,很久没更新了.毕业了,得好好装逼学习了,不能一心想着完了. 由于公司中的项目大量的使用DataTable,而每次对datatable进行操作的时候需要写很多相同的代码,所以秉着 装逼而学习 的态度,于是撸了几个扩展方法,记录下来,学习下.     class Program     {         public DataTable LinqTable = new DataTable();         void AddNewRow(int id, string name)   

DataTable扩展方法ToList&lt;T&gt;()、ToJSON()、ToArrayList()

/// <summary> /// 扩展方法类 /// </summary> public static class CommonExtension { /// <summary> /// 数据类型对应转换方法字典 /// </summary> static Dictionary<Type, Func<object, object>> dic_convert = new Dictionary<Type, Func<obje

扩展方法 DataTable To List&lt;T&gt;

生活很精彩,所以我自己创造自己的喜欢的东西,或取之有方.有时code累,重复的东西也多...也不是很美观,怎么样让自己更爽赶快顺手的编码呢....所以扩展方法也来了.. 一.扩展方法描述一方: 扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用. 对于用 C# 和 Visual Basic 编写的客户端代码,调用扩展方法与调用在类型中实际定义的方法之间没有明显的差异.扩展方法被

LINQ常用扩展方法

下面的方法都是IEnumerable<T>的扩展方法: Average计算平均值: Min最小元素:Max最大元素:Sum元素总和: Count元素数量: Concat连接两个序列://Unoin all Contains序列是否包含指定元素: Distinct取得序列中的非重复元素: Except获得两个序列的差集: Intersect获得两个序列的交集: First取得序列第一个元素: Single取得序列的唯一一个元素,如果元素个数不是1个,则报错:!!!严谨的程序. FirstOrDe

C#中的扩展方法详解

“扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.”这是msdn上说的,也就是你可以对String,Int,DataRow,DataTable等这些类型的基础上增加一个或多个方法,使用时不需要去修改或编译类型本身的代码. 扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用. 以上是msdn官网对扩展方法的描述,现在我通过一个情景

Farseer.net轻量级开源框架 中级篇:常用的扩展方法

导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: BasePage.BaseController.BaseHandler.BaseMasterPage.BaseControls基类使用 下一篇:Farseer.net轻量级开源框架 中级篇: 常用工具 在使用框架的时候,都需要引用扩展方法的命名空间:using FS.Extend; 1 /// <summary> 2 /// 将值转换成类型对像的值(此方法作为公共的调用,只支持

C#扩展方法的理解

“扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.” 这是msdn上说的,也就是你可以对String,Int,DataRow,DataTable等这些类型的基础上增加一个或多个方法,使用时不需要去修改或编译类型本身的代码. 先做个例子吧,以String为例,需要在字符串类型中加一个从字符串转为数值的功能. 以往我们可能是这样做的,会专门写一个方法做过转换 public static int StrToInt(string s) {     int