tags:

views:

208

answers:

4

The code below works unless p.School.SchoolName turns out to be null, in which case it results in a NullReferenceException.

if (ExistingUsers.Where(p => p.StudentID == item.StaffID &&
                        p.School.SchoolName == item.SchoolID).Count() > 0)
{
    // Do stuff.
}

ExistingUsers is a list of users:

public List<User> ExistingUsers;

Here is the relevant portion of the stacktrace:

System.NullReferenceException: Object reference not set to an instance of an object.

at System.Linq.Enumerable.WhereListIterator1.MoveNext()
at System.Linq.Enumerable.Count[TSource](IEnumerable
1 source)

How should I handle this where clause?

Thanks very much in advance.

+6  A: 

I suspect p.School is null, not SchoolName. Simply add a null check before accessing SchoolName. Also, use Any() to check if there are any results instead of Count() > 0 unless you're really in need of the count. This performs better since not all items are iterated if any exist.

var result = ExistingUsers.Where(p => p.StudentID == item.StaffID
                            && p.School != null
                            && p.School.SchoolName == item.SchoolID)
                         .Any();

if (result) { /* do something */ }
Ahmad Mageed
This worked like a charm. Many thanks, I have learned!
nestling
A: 

I think the error might be more that p.School is null. If so, add p.School != null as the first criteria after p => .

Anthony Pegram
A: 

In the case where you want to get the null value (all the student, with school or not) Use left join.

There are a good example on MSDN

Garcia Julien
A: 

If I remember correctly (not at my developer PC at the moment and can't check with Reflector), using the == operator results in calling the instance implementation string.Equals(string), not the static implementation String.Equals(string, string).

Assuming that your problem is due to SchoolName being null, as you suggest, try this:

if (ExistingUsers.Where(
    p => p.StudentID == item.StaffID 
    && String.Equals( p.School.SchoolName, item.SchoolID)).Count() > 0)
{
    // Do stuff.
}

Of course, comments by other answers count as well:

  • Using Any() instead of Count() > 0 will generally perform better
  • If p.School is the null, you'll need an extra check

Hope this helps.

Bevan