Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Oracle数据提供程序的奇怪行为(错误)(InvalidCastException:指定的强制转换无效)_C#_Oracle_Entity Framework_Exception_Oracle Manageddataaccess - Fatal编程技术网

C# Oracle数据提供程序的奇怪行为(错误)(InvalidCastException:指定的强制转换无效)

C# Oracle数据提供程序的奇怪行为(错误)(InvalidCastException:指定的强制转换无效),c#,oracle,entity-framework,exception,oracle-manageddataaccess,C#,Oracle,Entity Framework,Exception,Oracle Manageddataaccess,Oracle ManagedDataAccess似乎有问题,如果尝试执行以下示例,可能会出现错误(这是意外的;更改为其他字段但数据类型相同可能不会导致问题);尽管实体框架已实现查询,但没有问题: INVENTORY .GroupBy(inv => 1) .Select(invGroup => new { GrossWeight = invGroup.Sum(inv => inv.GROSS_WEIGHT / inv.BASE_QTY_PER_UNIT) }) 前面的

Oracle ManagedDataAccess似乎有问题,如果尝试执行以下示例,可能会出现错误(这是意外的;更改为其他字段但数据类型相同可能不会导致问题);尽管实体框架已实现查询,但没有问题:

INVENTORY
.GroupBy(inv => 1)
.Select(invGroup => new 
{
    GrossWeight = invGroup.Sum(inv => inv.GROSS_WEIGHT / inv.BASE_QTY_PER_UNIT)
})
前面的代码将导致以下异常:

   InvalidCastException
   Specified cast is not valid. 
   StackTrace
   at Oracle.ManagedDataAccess.Client.OracleDataReader.GetDecimal(Int32 i)
   at Oracle.ManagedDataAccess.Client.OracleDataReader.GetValue(Int32 i)
   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
   at lambda_method(Closure , Shaper )
   at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
   at UserQuery
在网上搜索,似乎其他人都有这个例外
有两种解决方法:

选项1 将所需值强制转换为float

INVENTORY
.GroupBy(inv => 1)
.Select(invGroup => new 
{
    GrossWeight = invGroup.Sum(inv => (float)inv.GROSS_WEIGHT / (float)inv.BASE_QTY_PER_UNIT)
})
选项2 使用AsEnumerable()在内存中执行求和,如下所示

INVENTORY
.GroupBy(inv => inv.BASE_QTY_PER_UNIT)
.Select(invGroup => new 
{
    GrossWeight = invGroup.Sum(inv => inv.GROSS_WEIGHT),
    BaseQuantityPerUnit = invGroup.Key
})
.AsEnumerable()
.Select(anony => new
{
    GrossWeight = anony.GrossWeight / anony.BaseQuantityPerUnit
})