Been too long since I put a post up, this little gotcha seems a good time to add to this.
Writing a lambda expression for work I couldn’t understand why I was getting no result back when there was obviously a result in the database. The lambda expression was similar to below :
var record = unitOfWork.Applications.Where( x => x.Code == "U" && x.Decision != "R" && x.UserID == userId).FirstOrDefault();
Seems a pretty straight forward statement, but no value being returned, I realised in this instance the value for the Decision field in the database record was null and so this was not equating, NULL != “R” is returning false so the solution was to check for the null. The change below fixed it using a ternary expression –> Expression : True : False
var record = unitOfWork.Applications.Where( x => x.Code == "U" && x.Decision == null ? true : x.Decision != "R" && x.UserID == userId).FirstOrDefault();
Simples
The post Entity Framework: Dealing with Nulls in Lambda Expressions appeared first on Net Awakening.