完整的错误消息:
强制转换为值类型'Boolean'失败,因为实例化值为null。结果类型的通用参数或查询必须使用可为空的类型。
码:
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
{...
}
StackTrace是:
在
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成型机)
System.Data.Common.Internal.Materialization.Shaper
1.SimpleEnumerator.MoveNext() at System.Linq.Enumerable.Single[TSource](IEnumerable
1源)System.Data.Objects.ELinq.ObjectQueryProvider.b__3 [TResult](IEnumerable
1 sequence) at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable
1查询,表达式queryRoot)在
在System.Linq.Queryable.Any [TSource](IQueryable`1源)处
最佳答案
tblMessageQueue
表中的列值之一可能是NULL
,而它在实体类中的对应属性是bool
类型。如果是这样,我可以想到以下两个建议:
更正数据和表,使其不包含或不允许NULL
将实体类属性更新为可空:bool?