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

  • 相关阅读:
    JavaScript 获取CSS媒体查询信息
    拖拉事件
    JavaScript 中的正常任务与微任务
    IOS 采用https 协议访问接口
    将类数组 转化为数组
    合并两个数组的方法
    base64转码
    Promise 异步执行的同步操作
    proxy set 拦截
    VIm 一些常用的设置
  • 原文地址:https://www.cnblogs.com/q975261413/p/4011869.html
Copyright © 2011-2022 走看看