I have following query:
var query = (from t1 in table1.Where(c => c.ServiceID == 2 && c.IsDeleted == 1)
from t2 in table2.Where(c => c.RelServiceBonID == t1.RelServiceBonID && c.IsDeleted == 1).DefaultIfEmpty()
from t3 in table3.Where(c => c.BonID == t1.BonID && c.UserID == 8 && c.IsDeleted == 1).DefaultIfEmpty()
from t4 in table4.Where(c => c.BonID == t1.BonID && c.IsDeleted == 1)
select new
{
t3.UserID, t2.SubServiceID,
});
query.Dump();
When I run, I got the following error:
The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.
The UserID
field is primary key and I can not set that as nullable!
How I can pass this error?
Best Answer
As hvd explained below, because you effectively have an outer join,
UserID
can still be null. Besides that,SubServiceID
can also be null. Usually it helps to cast the culprit to a nullable type: