• DataTable 转 IEnumerable


    互转【dt转化为List集合】

         /// <summary>
            /// DataTable 转换为List 集合
            /// </summary>
            /// <typeparam name="TResult">类型</typeparam>
            /// <param name="dt">DataTable</param>
            /// <returns></returns>
            public static List<TResult> ToList<TResult>(DataTable dt) where TResult : class, new()
            {
                //创建一个属性的列表
                List<PropertyInfo> prlist = new List<PropertyInfo>();
                //获取TResult的类型实例  反射的入口
                Type t = typeof(TResult);
                //获得TResult 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表 
                Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) prlist.Add(p); });
                //创建返回的集合
                List<TResult> oblist = new List<TResult>();
     
                foreach (DataRow row in dt.Rows)
                {
                    //创建TResult的实例
                    TResult ob = new TResult();
                    //找到对应的数据  并赋值
                    prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null); });
                    //放入到返回的集合中.
                    oblist.Add(ob);
                }
                return oblist;
            }
     
            /// <summary>
            /// 转换为一个DataTable
            /// </summary>
            /// <typeparam name="TResult"></typeparam>
            ///// <param name="value"></param>
            /// <returns></returns>
            public static DataTable ToDataTable(IEnumerable list) 
            {
                //创建属性的集合
                List<PropertyInfo> pList = new List<PropertyInfo>();
                //获得反射的入口
                Type type = list.AsQueryable().ElementType;
                DataTable dt = new DataTable();
                //把所有的public属性加入到集合 并添加DataTable的列
                Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });
                foreach (var item in list)
                {
                    //创建一个DataRow实例
                    DataRow row = dt.NewRow();
                    //给row 赋值
                    pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
                    //加入到DataTable
                    dt.Rows.Add(row);
                }
                return dt;
            }
     
     
            /// <summary>
            /// 转换为一个DataTable
            /// </summary>
            /// <typeparam name="TResult"></typeparam>
            ///// <param name="value"></param>
            /// <returns></returns>
            public static DataTable ToDataTable<TResult>(IEnumerable<TResult> value) where TResult : class
            {
                //创建属性的集合
                List<PropertyInfo> pList = new List<PropertyInfo>();
                //获得反射的入口
                Type type = typeof(TResult);
                DataTable dt = new DataTable();
                //把所有的public属性加入到集合 并添加DataTable的列
                Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });
                foreach (var item in value)
                {
                    //创建一个DataRow实例
                    DataRow row = dt.NewRow();
                    //给row 赋值
                    pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
                    //加入到DataTable
                    dt.Rows.Add(row);
                }
                return dt;
            }
    作者:chenze
    出处:https://www.cnblogs.com/chenze-Index/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    如果文中有什么错误,欢迎指出。以免更多的人被误导。
  • 相关阅读:
    Python Data Type
    Python 基础二
    Python的基础知识
    查看当前进程
    linux 通过服务名称查找目录
    在linux下登录MySQL,发现无法输入密码?
    多米诺骨牌效应
    MySQL MVCC底层原理详解MySQL MVCC底层原理详解
    C#保留小数点后几位
    在ubuntu服务器上安装mysql并配置外网访问
  • 原文地址:https://www.cnblogs.com/chenze-Index/p/11180538.html
Copyright © 2020-2023  润新知