0

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

在网上搜索,似乎其他人有这个例外 https://chrismay.org/2015/07/30/bug-in-oracledatareader-causing-invalidcastexception/

4

1 回答 1

0

有两种解决方法:

选项#1 将所需值转换为浮点数

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
})
于 2017-06-20T14:38:16.853 回答