/// <summary>
        /// DataSet转换成指定返回类型的实体集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static List<T> DataSetToList<T>(DataSet ds)
        {
            PropertyInfo[] properties = typeof(T).GetProperties();
            List<T> list = new List<T>();

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                T temp = System.Activator.CreateInstance<T>();
                foreach (PropertyInfo pro in properties)
                {
                    int colIndex = ds.Tables[0].Columns.IndexOf(pro.Name);
                    if (colIndex > -1)
                    {
                        if (pro.PropertyType == typeof(String))
                        {
                            pro.SetValue(temp, dr[colIndex].ToString(), null);
                        }
                        else if (pro.PropertyType == typeof(int))
                        {
                            pro.SetValue(temp, (int)dr[colIndex], null);
                        }
                        else if (pro.PropertyType == typeof(long))
                        {
                            pro.SetValue(temp, (long)dr[colIndex], null);
                        }
                    }
                }
                list.Add(temp);
            }
            return list;
        }

 

相关文章:

  • 2021-12-30
  • 2022-03-04
  • 2022-12-23
  • 2022-02-20
  • 2022-02-01
  • 2021-10-05
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-14
  • 2022-12-23
  • 2022-12-23
  • 2021-08-21
  • 2021-09-02
  • 2021-06-13
相关资源
相似解决方案