C#DataTable转List

方法一:

 /// <summary>
        /// DataTable转List<T>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<T> DtToList<T>(this DataTable dt) where T : class, new()
        {
            //属性列表
            List<System.Reflection.PropertyInfo> ls_pro = new List<System.Reflection.PropertyInfo>();

            Type t = typeof(T);
            //把T里所有public属性字段和dt列名一样的放进ls_pro属性列表存放
            Array.ForEach<System.Reflection.PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) ls_pro.Add(p); });
            //Array.ForEach<System.Reflection.PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.Contains(p.Name)) ls_pro.Add(p); });
            List<T> ls_t = new List<T>();
            foreach (DataRow dr in dt.Rows)
            {
                T ob = new T();
                //遍历出属性列表里字段在dt里的值(非空即为有值)
                ls_pro.ForEach(p => { if (dr[p.Name] != DBNull.Value) p.SetValue(ob, dr[p.Name], null); });
                ls_t.Add(ob);
            }
            return ls_t;
        }

 

方法二:

 /// <summary>
        /// Datable转List,可转T类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<T> ToList<T>(this DataTable dt) where T:class,new()
        {
            Type t = typeof(T);
            System.Reflection.PropertyInfo[] pi = t.GetProperties();//读取T里public属性字段
            List<T> ls = new List<T>();//存放转换结果
            string tname = string.Empty;
            foreach (DataRow dr in dt.Rows)
            {
                T entity = new T();
                foreach (var p in pi)
                {
                    tname = p.Name;
                    if (dt.Columns.Contains(tname))
                    {
                        if (!p.CanWrite) continue;
                        object value = dr[tname];
                        if (p.PropertyType == typeof(string))
                        {
                            p.SetValue(entity,value.ToString(),null);
                        }
                        else if (p.PropertyType == typeof(int) || p.PropertyType == typeof(int?))
                        {
                            p.SetValue(entity, (int)value, null);
                        }
                        else if (p.PropertyType == typeof(decimal) || p.PropertyType == typeof(decimal?))
                        {
                            p.SetValue(entity, (decimal)value, null);
                        }
                        else if (p.PropertyType == typeof(float) || p.PropertyType == typeof(float?))
                        {
                            p.SetValue(entity, (float)value, null);
                        }
                        else if (p.PropertyType == typeof(bool))
                        {
                            p.SetValue(entity, (bool)value, null);
                        }
                        else if (p.PropertyType == typeof(DateTime) || p.PropertyType == typeof(DateTime?))
                        {
                            p.SetValue(entity, (DateTime)value, null);
                        }
                        else
                        {
                            p.SetValue(entity, value, null);
                        }
                    }
                }
                ls.Add(entity);
            }
            return ls;
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值