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/
答案 0 :(得分:0)
有两种解决方法:
选项#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
})