LinqResult.Any() Resulting In NullReferenceException
Question
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
}
Solution
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?
OTHER TIPS
Most likely either Db
or Db.Table
are null
.
It is probably that Db is null. The exception happens when you do the .Any(), but this is because of defered execution.
The Db.Table
value is null
.
It is not Db
being null
as other people have suggested. Else the exception would have occured on the actual query.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow