The FirstOrDefault
method returns null
, if there is no item.
You have to check the value that is returned from from the FirstOrDefault
method, before using it. Something like the following:
var orderDetail = (from u in db.aspnet_Users
where u.UserName == User.Identity.Name
join d in db.Doctors on u.UserId equals d.UserId
join o in db.OrderDetails on d.DoctorsId equals o.DoctorsId
select o).FirstOrDefault();
var query = (orderDetail != null) ? orderDetail.Quantity : 0;