完整的错误信息:
The cast to value type 'Boolean' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
代码:
var messages = ctx.tblMessageQueue.Where(o => o.Status == status && o.Region == region);
//o.Status is a byte type and not nullable, o.Region is an int type and not nullable.
if(messages != null && messages.Any()) => Triggers the Error
{...
}
堆栈跟踪是:
at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader
1.GetValue(DbDataReader reader, Int32 ordinal) at lambda_method(Closure , Shaper ) at System.Data.Common.Internal.Materialization.Coordinator
1.ReadNextElement(Shaper shaper) at System.Data.Common.Internal.Materialization.Shaper1.SimpleEnumerator.MoveNext() at System.Linq.Enumerable.Single[TSource](IEnumerable
1 source) at System.Data.Objects.ELinq.ObjectQueryProvider.b__3[TResult](IEnumerable1 sequence) at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable
1 query, Expression queryRoot) at at System.Linq.Queryable.Any[TSource](IQueryable`1 source)
最佳答案
也许您的 tblMessageQueue
表中的列值之一是 NULL
,而它在实体类中的对应属性是 bool
类型。如果是这样的话,这里有两个我能想到的建议:
- 更正数据和表格,使其不包含或不允许
NULL
- 将实体类属性更新为可为空:
bool?
关于c# - .Any 导致转换为值类型 'Boolean' 失败,因为物化值为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21128669/