• 将DataTable转换为List<T>对象遇到问题:类型“System.Int64”的对象无法转换为类型“System.Int32”。


    可以利用反射将DataTable转换为List<T>对象:原始链接http://www.jb51.net/article/67386.htm

    但是该方法在DataTable里某个字段类型是Int32会有问题,报异常:类型“System.Int64”的对象无法转换为类型“System.Int32”。

    可在赋值的时候加一句:

    if(pi.GetMethod.ReturnParameter.ParameterType.Name == "Int32")
    {
    value = Convert.ToInt32(value);
    }
    pi.SetValue(t, value, null);

    整体代码:

     1 /// <summary> 
     2 /// 利用反射将DataTable转换为List<T>对象
     3 /// </summary> 
     4 /// <param name="dt">DataTable 对象</param> 
     5 /// <returns>List<T>集合</returns> 
     6 public static List<T> DataTableToList<T>(DataTable dt) where T : class, new()
     7 {
     8 // 定义集合 
     9 List<T> ts = new List<T>();
    10 //定义一个临时变量 
    11 string tempName = string.Empty;
    12 //遍历DataTable中所有的数据行 
    13 foreach (DataRow dr in dt.Rows)
    14 {
    15 T t = new T();
    16 // 获得此模型的公共属性 
    17 PropertyInfo[] propertys = t.GetType().GetProperties();
    18 //遍历该对象的所有属性 
    19 foreach (PropertyInfo pi in propertys)
    20 {
    21 tempName = pi.Name;//将属性名称赋值给临时变量 
    22 //检查DataTable是否包含此列(列名==对象的属性名) 
    23 if (dt.Columns.Contains(tempName))
    24 {
    25 //取值 
    26 object value = dr[tempName];
    27 //如果非空,则赋给对象的属性 
    28 if (value != DBNull.Value)
    29 {
    30 if (pi.GetMethod.ReturnParameter.ParameterType.Name == "Int32")
    31 {
    32 value = Convert.ToInt32(value);
    33 }
    34 pi.SetValue(t, value, null);
    35 }
    36 }
    37 }
    38 //对象添加到泛型集合中 
    39 ts.Add(t);
    40 }
    41 return ts;
    42 }

    其他类型转换汇总

     1 public static  List<T> ToList<T>(this DataTable dt) where T:class,new()
     2 {
     3 Type t=typeof(T);
     4 PropertyInfo[] propertys = t.GetProperties();
     5 List<T> lst = new List<T>();
     6 string typeName = string.Empty;
     7 foreach (DataRow dr in dt.Rows)
     8 {
     9 T entity = new T();
    10 foreach (PropertyInfo pi in propertys)
    11 {
    12 typeName = pi.Name;
    13 if (dt.Columns.Contains(typeName))
    14 {
    15 if (!pi.CanWrite) continue;
    16 object value = dr[typeName];
    17 if (value == DBNull.Value) continue;
    18 if (pi.PropertyType == typeof(string))
    19 {
    20 pi.SetValue(entity,value.ToString(),null);
    21 }
    22 else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(int?))
    23 {
    24 pi.SetValue(entity,int.Parse(value.ToString()), null);
    25 }
    26 else if (pi.PropertyType == typeof(DateTime?) || pi.PropertyType == typeof(DateTime))
    27 {
    28 pi.SetValue(entity, DateTime.Parse(value.ToString()), null);
    29 }
    30 else if (pi.PropertyType == typeof(float))
    31 {
    32 pi.SetValue(entity, float.Parse(value.ToString()), null);
    33 }
    34 else if (pi.PropertyType == typeof(double))
    35 {
    36 pi.SetValue(entity, double.Parse(value.ToString()), null);
    37 }
    38 else
    39 {
    40 pi.SetValue(entity,value, null);
    41 }
    42 }
    43 }
    44 lst.Add(entity);
    45 }
    46 return lst;
    47 }
  • 相关阅读:
    蛇形填数(算法竞赛入门经典)
    35. Search Insert Position(LeetCode)
    70. Climbing Stairs(LeetCode)
    循环结构程序设计(算法竞赛入门经典)课后题
    阶乘之和(算法竞赛入门经典)[求余问题]
    有关int范围的例题(算法竞赛入门经典)
    矩阵行成列,列成行
    543. Diameter of Binary Tree(LeetCode)
    415. Add Strings(LeetCode)
    121. Best Time to Buy and Sell Stock(LeetCode)
  • 原文地址:https://www.cnblogs.com/Mindy-hym/p/6187287.html
Copyright © 2020-2023  润新知