[英]Linq error: The cast to value type 'Int32' failed because the materialized value is null
... But it's not null. ...但是它不是空的。
FYI - Many threads exist on this error, but none that I've seen using an anonymous type. 仅供参考-此错误存在许多线程,但是使用匿名类型我见不到。
I'm Getting an odd InvalidOperationException in a Linq query. 我在Linq查询中收到一个奇怪的InvalidOperationException。
message: "The cast to value type 'Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type." 消息:“强制转换为值类型'Int32',因为实例化值为null。结果类型的通用参数或查询必须使用可为空的类型。”
The confusing thing is that it's erroring when creating an anonymous type: 令人困惑的是,创建匿名类型时会出错:
var workstepid = 484,449;
var wsData = ion.xWorkSteps
.Where(w => w.WorkStepId == workstepId)
.Select(w => new
{
w.WorkStepId,
w.ServiceId,
w.Service.TitleId,
w.Service.Title.OrderId,
w.Service.Title.AltTitleId
}).SingleOrDefault();
In LinqPad this particular query runs just fine and the workstepId used returns an integer value for each property of the anonymous type. 在LinqPad中,此特定查询运行良好,并且使用的workstepId为匿名类型的每个属性返回一个整数值。 So why a casting error when there is no null value for any property!?
那么,为什么在没有任何属性的空值时出现转换错误呢?
FYI, the last property AltTitleId is a nullable int, and the other properties are ints. 仅供参考,最后一个属性AltTitleId是可为null的int,其他属性为int。 Also, this code was written weeks ago and I didn't get this error until today.
另外,这段代码是在几周前编写的,直到今天我才得到这个错误。 Is something funky w/ my EF?
我的EF是时髦的东西吗?
Edit: SOLVED 编辑:解决
We use soft-delete in our db's, setting deleted records to Acive=0. 我们在数据库中使用软删除,将已删除的记录设置为Acive = 0。 It turns out that there is a property set in EF on the edmx tables filtering out inactive records (filter for Active=1).
事实证明,在edmx表上的EF中设置了一个属性,用于过滤掉不活动的记录(Active = 1的过滤器)。 Naturally, this wouldn't effect Linqpad which gives me the expected result, but since this was an older record I was testing, the title record (Service.Title) had been marked Active=0 and was therefore returning null.
自然,这不会影响Linqpad的效果,后者会给我预期的结果,但是由于这是我正在测试的较旧记录,因此标题记录(Service.Title)已被标记为Active = 0,因此返回null。
Thanks to everyone for the help. 感谢大家的帮助。
var workstepid = 484,449;
var wsData = ion.xWorkSteps
.Where(w => w.WorkStepId == workstepId)
.Select(w => new
{
w.WorkStepId.Value,
w.ServiceId.Value,
w.Service.TitleId.Value,
w.Service.Title.OrderId.Value,
w.Service.Title.AltTitleId.Value
}).SingleOrDefault();
Hi you need to add .Value in your int data type to accept null value. 嗨,您需要在int数据类型中添加.Value以接受空值。 hope this helps
希望这可以帮助
如果w.WorkStepId
可为空,请尝试将变量设置为...
int? workstepid = 484449;
I suppose, the problem is in your SingleOrDefault(). 我想,问题出在您的SingleOrDefault()中。
I suppose that you get 0 records: 我想您得到0条记录:
var workstepid = 484,449;
var len = ion.xWorkSteps
.Where(w => w.WorkStepId == workstepId)
.Length();
And then the anonymouse type must be replace by Default (this is what SingleOrDefault do), but there is no default for your anonymous type. 然后必须用默认值替换匿名类型(这是SingleOrDefault所做的事情),但是匿名类型没有默认值。
Try to change it to: 尝试将其更改为:
var wsData = ion.xWorkSteps
.Where(w => w.WorkStepId == workstepId)
.Select(w => new
{
w.WorkStepId,
w.ServiceId,
w.Service.TitleId,
w.Service.Title.OrderId,
w.Service.Title.AltTitleId
})
.Take(1)
.ToArray();
May be there are no Service/Title/Order id somewhere, but if it declared as int anonymous type expect int (not nullable), but from database it returns null. 可能在某处没有Service / Title / Order id,但是如果将其声明为int匿名类型,则应为int(不可为空),但是从数据库中它将返回null。 Try to rewrite as :
尝试重写为:
var workstepid = 484,449;
var wsData = ion.xWorkSteps
.Where(w => w.WorkStepId == workstepId)
.Select(w => new
{
WorkStepId = (int?)w.WorkStepId,
ServiceId = (int?)w.ServiceId,
TitleId = (int?)w.Service.TitleId,
OrderId = (int?)w.Service.Title.OrderId,
w.Service.Title.AltTitleId
}).SingleOrDefault();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.