对DataSet,DataRow,DateTable转换成相应的模型

        /// <summary>
        /// DataRow 转成 模型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dr"></param>
        /// <returns></returns>
        public static T ToModel<T>(this DataRow dr) where T : class, new()
        {
            T ob = new T();
            if (dr != null)
            {
                Type vType = typeof(T);
                //创建一个属性的列表
                PropertyInfo[] prlist = vType.GetProperties();


                DataColumnCollection vDataCoulumns = dr.Table.Columns;
                try
                {
                    foreach (PropertyInfo vProInfo in prlist)
                    {
                        if (vDataCoulumns.IndexOf(vProInfo.Name) >= 0 && dr[vProInfo.Name] != DBNull.Value)
                        {
                            vProInfo.SetValue(ob, dr[vProInfo.Name], null);
                        }
                    }
                }
                catch (Exception ex)
                {


                }
            }
            return ob;
        }
        /// <summary>
        /// DataTable 转换为List 集合
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="dt">DataTable</param>
        /// <returns></returns>
        public static List<T> ToList<T>(this DataTable dt) where T : class, new()
        {
            //创建一个属性的列表
            List<PropertyInfo> prlist = new List<PropertyInfo>();
            //获取TResult的类型实例  反射的入口
            Type t = typeof(T);
            //获得TResult 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表 
            Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) prlist.Add(p); });
            //创建返回的集合
            List<T> oblist = new List<T>();
            if (dt != null)
            {
                foreach (DataRow row in dt.Rows)
                {
                    try
                    {
                        //创建TResult的实例
                        T ob = new T();
                        //找到对应的数据  并赋值
                        prlist.ForEach(p =>
                        {
                            if (dt.Columns.IndexOf(p.Name) >= 0 && row[p.Name] != DBNull.Value)
                            {
                                try
                                {
                                    //如果是泛型
                                    if (p.PropertyType.ToString().IndexOf("System.Nullable") > -1)
                                    {
                                        string types = p.PropertyType.ToString().Substring(p.PropertyType.ToString().IndexOf("[") + 1);
                                        types = types.Substring(0, types.Length - 1);


                                        Type typeinfo = Type.GetType(types);
                                        p.SetValue(ob, Convert.ChangeType(row[p.Name], typeinfo), null);


                                    }
                                    else
                                    {
                                        p.SetValue(ob, Convert.ChangeType(row[p.Name], p.PropertyType), null);//类型转换
                                    }
                                }
                                catch (Exception ex)
                                {
                                    LogUtility.WriteLogForExcepiton(ex);
                                }


                            }
                        });
                        //放入到返回的集合中.
                        oblist.Add(ob);
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
            return oblist;
        }
        /// <summary>         
        /// DataSet转换为泛型集合         
        /// </summary>         
        /// <typeparam name="T">泛型类型</typeparam>         
        /// <param name="ds">DataSet数据集</param>         
        /// <param name="tableIndex">待转换数据表索引,默认第0张表</param>         
        /// <returns>返回泛型集合</returns>         
        public static IList<T> ToList<T>(this DataSet ds, int tableIndex = 0)
        {

            if (ds == null || ds.Tables.Count < 0) return null;
            if (tableIndex > ds.Tables.Count - 1)
                return null;

            if (tableIndex < 0)
                tableIndex = 0;

            DataTable dt = ds.Tables[tableIndex];

            // 返回值初始化             
            IList<T> result = new List<T>();
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                T _t = (T)Activator.CreateInstance(typeof(T));
                PropertyInfo[] propertys = _t.GetType().GetProperties();

                foreach (PropertyInfo pi in propertys)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {

                        // 属性与字段名称一致的进行赋值                         
                        if (pi.Name.Equals(dt.Columns[i].ColumnName))
                        {

                            // 数据库NULL值单独处理                             
                            if (dt.Rows[j][i] != DBNull.Value)
                                pi.SetValue(_t, dt.Rows[j][i], null);
                            else
                                pi.SetValue(_t, null, null);
                            break;
                        }
                    }
                }
                result.Add(_t);
            }
            return result;
        }


        /// <summary>         
        /// DataSet转换为泛型集合         
        /// </summary>         
        /// <typeparam name="T">泛型类型</typeparam>         
        /// <param name="ds">DataSet数据集</param>         
        /// <param name="tableName">待转换数据表名称,名称为空时默认第0张表</param>         
        /// <returns>返回泛型集合</returns>         
        public static IList<T> ToList<T>(this DataSet ds, string tableName)
        {

            int _TableIndex = 0;

            if (ds == null || ds.Tables.Count < 0)
                return null;

            if (string.IsNullOrEmpty(tableName))
                return ToList<T>(ds, 0);

            for (int i = 0; i < ds.Tables.Count; i++)
            {

                // 获取Table名称在Tables集合中的索引值                 
                if (ds.Tables[i].TableName.Equals(tableName))
                {
                    _TableIndex = i;
                    break;
                }
            }
            return ToList<T>(ds, _TableIndex);
        }


猜你喜欢

转载自blog.csdn.net/qq389216533/article/details/79815332