zoukankan      html  css  js  c++  java
  • asp.net core 3.1 EFcore 执行sql,datatable转List对象时 Object of type 'System.UInt64' cannot be converted to type 'System.Boolean'.

     public static IList<T> DataTableToIList<T>(DataTable dt)
            {
                if (dt == null)
                    return null;
    
                DataTable p_Data = dt;
                // 返回值初始化
                IList<T> result = new List<T>();
                for (int j = 0; j < p_Data.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 < p_Data.Columns.Count; i++)
                        {
                            // 属性与字段名称一致的进行赋值
                            if (pi.Name.Equals(p_Data.Columns[i].ColumnName))
                            {
                                // 数据库NULL值单独处理
                                if (p_Data.Rows[j][i] != DBNull.Value)
                                {
                                    if(pi.PropertyType.FullName == "System.Boolean")
                                    {
                                        var val = p_Data.Rows[j][i].ToString() == "1" ? true : false;
                                        pi.SetValue(_t, val, null);
                                    }
                                    else
                                    {
                                        pi.SetValue(_t, p_Data.Rows[j][i], null);
                                    }
                                    
                                } 
                                else
                                {
                                    pi.SetValue(_t, null, null);
                                }
                                   
                                break;
                            }
                        }
                    }
                    result.Add(_t);
                }
                return result;
            }

    目前不清楚具体的原因,用上面的方法来规避一下。

  • 相关阅读:
    web动静分离
    vm采用NAT方式连接时,设置静态ip
    nginx实现tcp负载均衡
    读取文件
    线程池源码分析
    mongodb操作
    bind
    Xss攻击
    json和java对象相互转换
    静态资源默认加载路径
  • 原文地址:https://www.cnblogs.com/puzi0315/p/13511284.html
Copyright © 2011-2022 走看看