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;
        }

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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM