zoukankan      html  css  js  c++  java
  • 【转】将datatable数据转化成list

    #region 将datatable数据转化成list   public static List<T> ToList<T>(this DataTable dt) where T : class,new()
            /// <summary>
            /// 将datatable数据转化成list
            /// </summary>
            /// <typeparam name="T">泛型T</typeparam>
            /// <param name="dt">对应的datatable数据表</param>
            /// <returns>返回结果的数据集</returns>
            public static List<T> ToList<T>(this DataTable dt) where T : class,new()
            {
                Type t = typeof(T);
                PropertyInfo[] propertys = t.GetProperties();
                List<T> lst = new List<T>();
                string typeName = string.Empty;


                foreach (DataRow dr in dt.Rows)
                {
                    T entity = new T();
                    foreach (PropertyInfo pi in propertys)
                    {
                        typeName = pi.Name;
                        if (dt.Columns.Contains(typeName))
                        {
                            if (!pi.CanWrite) continue;
                            object value = dr[typeName];
                            if (value == DBNull.Value) continue;
                            if (pi.PropertyType == typeof(string))
                            {
                                pi.SetValue(entity, value.ToString(), null);
                            }
                            else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(int?))
                            {
                                pi.SetValue(entity, int.Parse(value.ToString()), null);
                            }
                            else if (pi.PropertyType == typeof(DateTime?) || pi.PropertyType == typeof(DateTime))
                            {
                                pi.SetValue(entity, DateTime.Parse(value.ToString()), null);
                            }
                            else if (pi.PropertyType == typeof(float))
                            {
                                pi.SetValue(entity, float.Parse(value.ToString()), null);
                            }
                            else if (pi.PropertyType == typeof(double))
                            {
                                pi.SetValue(entity, double.Parse(value.ToString()), null);
                            }
                            else
                            {
                                pi.SetValue(entity, value, null);
                            }
                        }
                    }
                    lst.Add(entity);
                }
                return lst;


                //调用
                //List<People> p = dt.ToList<People>();
            } 
            #endregion

  • 相关阅读:
    07 总结ProgressDialog 异步任务
    1. vim 的安装及配置
    debian 源设置 ( apt-get 不能安装)
    在Debian中安装VNC Server
    让BB-Black通过usb0上网
    常用的一些 linux 指令
    Linux下同一目录内文件和目录为什么不能同名?
    beaglebone black 与电脑互传文件(夹)
    永久修改 putty字体大小
    Beaglebone Black的引脚分配
  • 原文地址:https://www.cnblogs.com/q975261413/p/4011869.html
Copyright © 2011-2022 走看看