sooprise
sooprise

Reputation: 23207

LinqResult.Any() Resulting In NullReferenceException

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
}

Upvotes: 1

Views: 318

Answers (4)

JaredPar
JaredPar

Reputation: 755587

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.

Upvotes: 0

Phillip Ngan
Phillip Ngan

Reputation: 16126

It is probably that Db is null. The exception happens when you do the .Any(), but this is because of defered execution.

Upvotes: 0

LukeH
LukeH

Reputation: 269658

Most likely either Db or Db.Table are null.

Upvotes: 0

Brian Driscoll
Brian Driscoll

Reputation: 19635

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?

Upvotes: 1

Related Questions