LinqResult.Any() 导致 NullReferenceException
可能导致此错误的原因:
NullReferenceException 未处理,未将对象引用设置为对象的实例。
var LinqResult =
from a in Db.Table
select new {Table = a};
if(LinqResult.Any())
{
//Blah blah blah
}
What could be causing this error:
NullReferenceException was unhanded, Object reference not set to an instance of an object.
var LinqResult =
from a in Db.Table
select new {Table = a};
if(LinqResult.Any())
{
//Blah blah blah
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的猜测是 Db 或 Db.Table 在执行该查询时尚未实例化。您可以发布任何额外的上下文代码吗?
My guess is that either Db or Db.Table has not yet been instantiated at the point of execution of that query. Can you post any additional code for context?
Db
或Db.Table
很可能为null
。Most likely either
Db
orDb.Table
arenull
.很可能 Db 为空。当您执行 .Any() 时会发生异常,但这是因为延迟执行。
It is probably that Db is null. The exception happens when you do the .Any(), but this is because of defered execution.
Db.Table
值为null
。正如其他人所建议的那样,
Db
并不null
。否则,实际查询中就会发生异常。The
Db.Table
value isnull
.It is not
Db
beingnull
as other people have suggested. Else the exception would have occured on the actual query.