gpt4 book ai didi

c# - Queryable.Any() 返回空值?

转载 作者:太空宇宙 更新时间:2023-11-03 10:41:20 25 4
gpt4 key购买 nike

我有一个数据库查询:

var configs = dbData.Configs.Where(e => headers.Contains(e.headerId) && e.flag == "true");
if(configs.Any())
{ ... }

其中 configs 作为 IQueryable 类型返回。我有时会收到此错误:

System.InvalidOperationException: 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. at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader1.GetValue(DbDataReader
reader, Int32 ordinal) at
System.Data.Common.Internal.Materialization.Shaper.GetColumnValueWithErrorHandling[TColumn](Int32
ordinal) 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.Linq.Queryable.Any[TSource](IQueryable`1 source)

它发生在生产中,似乎是随机的,我无法在开发中重现它。

编辑添加:Config 类型有一个外键约束,如果 bool 类型是从中转换的,我不知道。 Entity Framework 创建的 Config 类型中没有 Boolean,Sql Server 中也没有 Boolean 类型。

最佳答案

您的 Configs 表中有一些数据库列具有可为 null 的 bool 类型,但您将表条目映射到的 C# 实体被定义为使用不可为 null 的 bool 值。当查询提供程序填充 C# 实体时,当它遇到 null 值时,它会抛出此异常。您需要更新实体对象,以便相关列的类型正确。

关于c# - Queryable.Any() 返回空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25246178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com