'LINQ not evaluating check for null object
I'm wondering why my linq statement doesn't properly evaluate the null check on the Agency object which is a property of the Users model.
var invalidUsers = this.DbContext.Users.Where(p => p.Agency != null).ToList();
var invalidUsersList = invalidUsers.Where(p => p.Agency != null).ToList();
When I run the code above, the first line returns a list of all Users, regardless of the Agency object being null or not. However, the second line executes and properly filters the list and returns a list that is correctly returning Users where Agency is not null.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
