I am using the following code to extract data from my database using entities. If a record isn’t found it throws the following exception “Object reference not set to an instance of an object.” I can catch this to stop it causing problems but would rather modify the code to not have the problem. Can I change the Linq query so that it is more tolerant ?
using (var ctx = new MyEntities())
{
var users = ctx.NotificationMessages.Include("NotificationUsers")
.Where(x => x.Priority == priority)
.FirstOrDefault().NotificationUsers
.ToList();
}