我有以下查询:
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();
当我跑步时,我收到以下错误:
无法将null值分配给System.Int32类型的成员,该类型是不可为空的值类型。
UserID
字段是主键,我无法将其设置为可为空!
我如何传递此错误?
答案 0 :(得分:9)
UserID字段是主键,我无法将其设置为可为空的
正如hvd在下面解释的那样,因为你实际上有一个外连接,UserID
仍然可以为空。除此之外,SubServiceID
也可以为空。通常它有助于将罪魁祸首变为可以为空的类型:
select new
{
UserID = (int?)t3.UserID,
SubServiceID = (int?)t2.SubServiceID
});