linq where clause and count result in null exception.
Posted
by nestling
on Stack Overflow
See other posts from Stack Overflow
or by nestling
Published on 2010-03-23T04:10:49Z
Indexed on
2010/03/23
4:21 UTC
Read the original article
Hit count: 567
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.WhereListIterator
1.MoveNext()
1 source)
at System.Linq.Enumerable.Count[TSource](IEnumerable
How should I handle this where clause?
Thanks very much in advance.
© Stack Overflow or respective owner