public static T GetEntity<T>(DataTable table) where T : new()
    {
        T entity = new T();
        foreach (DataRow row in table.Rows)
        {
            foreach (var item in entity.GetType().GetProperties())
            {
                if (row.Table.Columns.Contains(item.Name))
                {
                    if (DBNull.Value != row[item.Name])
                    {
                        item.SetValue(entity, Convert.ChangeType(row[item.Name], item.PropertyType), null);
                    }

}
            }
        }

return entity;
    }

dataTable转化为实体列表MethodOne:

public static IList<T> GetEntities<T>(DataTable table) where T : new()
    {
        IList<T> entities = new List<T>();
        foreach (DataRow row in table.Rows)
        {
            T entity = new T();
            foreach (var item in entity.GetType().GetProperties())
            {
                item.SetValue(entity, Convert.ChangeType(row[item.Name], item.PropertyType), null);
            }
            entities.Add(entity);
        }
        return entities;
    }

MethodTwo:

/// <summary>
        /// DataTable 转化为对象集合
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<TEntity> ToList<TEntity>(DataTable dt) where TEntity : new()
        {
            List<TEntity> listEntity = new List<TEntity>();
            if (dt != null && dt.Rows.Count > 0)
            {
                int fieldCount = dt.Columns.Count;
                foreach (DataRow item in dt.Rows)
                {
                    TEntity t = (TEntity)Activator.CreateInstance(typeof(TEntity));

for (int i = 0; i < fieldCount; i++)
                    {
                        PropertyInfo field = t.GetType().GetProperty(dt.Columns[i].ColumnName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                        if (field != null)
                        {
                            if (item[i] == null || Convert.IsDBNull(item[i]))
                            {
                                field.SetValue(t, null, null);
                            }
                            else
                            {
                                field.SetValue(t, item[i], null);
                            }
                        }
                    }

listEntity.Add(t);
                }
            }
            if (listEntity.Count == 0)
            {
                return null;
            }

return listEntity;
        }

C# DataTable转实体 通用方法【转】的更多相关文章

  1. C# DataTable转实体通用方法

    public static class DataTableHelper { public static T GetEntity<T>(DataTable table) where T : ...

  2. C# DataTable转实体 通用方法

    public static T GetEntity<T>(DataTable table) where T : new() { T entity = new T(); foreach (D ...

  3. 三层架构中bll层把datatable转换为实体model的理解

    看了很多人的项目,很多都是用到三层架构,其中BLL层中有一种将DataTable转换为实体的方法.一直没有明白为啥要这样做,今天特意去搜索了一下,如果没有答案我是准备提问,寻求解答了.还好找到一个相关 ...

  4. 使用 AutoMapper 映射 IDataReader、DataSet、DataTable 到实体类

    AutoMapper是一个.NET的对象映射工具. 项目地址:https://github.com/AutoMapper/AutoMapper. 帮助文档:https://github.com/Aut ...

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

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

  6. DataTable数据赋值给Model通用方法

    注:该文属本人原创,今后项目中发现该方法存在BUG会实时更新,转载记得附上原文出处,方便大家获得最新代码. 相信大家在做项目中,经常会根据不同的表new各种不同的Model,当需要对Model进行实例 ...

  7. datatable与实体类之间相互转化的几种方法

    #region DataTable转换成实体类 /// <summary> /// 填充对象列表:用DataSet的第一个表填充实体类 /// </summary> /// & ...

  8. DataTable 转实体

    因为Linq的查询功能很强大,所以从数据库中拿到的数据为了处理方便,我都会转换成实体集合List<T>. 开始用的是硬编码的方式,好理解,但通用性极低,下面是控件台中的代码: using ...

  9. 自用的基于Emit的C#下DataTable转实体类方法

    之前一直在做WebForm的开发,数据绑定时直接DataTable绑定Gridview很方便,但是最近开始往MVC转,数据列表的传递和页面展示基本上是以List为主,像下面这样,遍历实体类的各个字段去 ...

随机推荐

  1. Ubuntu下Eclipse搭建ARM开发环境

    第一步:安装JRE 和 Eclipse 详细步骤请参考:http://blog.csdn.net/ex_net/article/details/7251664 第二步:安装arm-linux-gcc ...

  2. stl入门--reverse函数

    #include<iostream> #include<algorithm>          using namespace std; int main() {     ch ...

  3. 电子科大POJ "a^b"

    a ^ b Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) C-sources: ...

  4. 【POJ 1679 The Unique MST】最小生成树

    无向连通图(无重边),判断最小生成树是否唯一,若唯一求边权和. 分析生成树的生成过程,只有一个圈内出现权值相同的边才会出现权值和相等但“异构”的生成树.(并不一定是最小生成树) 分析贪心策略求最小生成 ...

  5. Linux 零碎知识点

    ln -s ../libs/ libs 在当前目录下建立一个符号链接文件libs,使它指向上一层目录的libs文件夹 关于su和su -的区别切换用户是可以使用su tom或者su - tom来实现, ...

  6. 移动端页面SEO优化需要注意的10个要点

    如今,移动互联网已经成为互联网组成的非常重要的一个分支,如果说以前对移动页面没有很规范的优化和高质量内容评判划分标准,但现在随着各大搜索引擎发布了移动建站指南,图文并茂的描述了如何提高移动站在百度质量 ...

  7. 安装android studio时,解决unable to access android sdk add-on list

    1.打开时提示如下错误. 2.在安装路径下找到idea.properties文件,用记事本打开,添加disable.android.first.run=true,保存即可. 3.再次打开Android ...

  8. Buns(dp+多重背包)

    C. Buns time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  9. c++冒泡排序的模板函数设计

    说明 由于课程设计需要,特编写本程序.本程序首先定义了一个冒泡程序的模板函数,然后在main()函数中定义了两个不同类型的数组,调用模板函数对其进行排序.(注意,本程序是在linux下编写,但是直接拷 ...

  10. C++ 顶层 const

    我的主力博客:半亩方塘 本文的主要參考来源来自于:C++ Primer 中文版(第 5 版) 第 57 面至第 58 面 1. 顶层 const 与底层 const 概念 我们知道,指针本身是一个对象 ...