Tom
Tom

Reputation: 1071

LINQ To Entities .Any() results in null reference exception

I have a simple query as below:

Dim sizings = From a In db.Sizings
                      Where a.Customer.ID = customer.ID
                      Select a

If sizings.Any Then
    .....

The sizings.Any line is throwing a null reference exception. I thought I was meant to use .Any to determine if there were any rows returned?

isnothing(sizings) returns false.

Any ideas?

Edit - Resolution: Don't use null objects in the LINQ Query!

Upvotes: 3

Views: 2081

Answers (2)

p.campbell
p.campbell

Reputation: 100587

Try checking that Customer isn't null before comparing its ID.

Dim sizings = From a In db.Sizings
              Where a.Customer IsNot Nothing And a.Customer.ID = customer.ID
              Select a

If sizings.Any() Then
   '
End If

Upvotes: 2

Jen Grant
Jen Grant

Reputation: 2074

What about using sizings.Count() > 0

Upvotes: 0

Related Questions