我有以下查询:

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();

运行时,出现以下错误:


UserID字段是主键,我不能将其设置为可为空!

如何传递此错误?

最佳答案



如下面的hvd所述,由于您实际上具有外部联接,因此UserID仍可以为null。除此之外,SubServiceID也可以为null。通常,这有助于将罪魁祸首转换为可为null的类型:

 select new
 {
     UserID = (int?)t3.UserID,
     SubServiceID = (int?)t2.SubServiceID
 });

关于c# - LINQ错误: The null value cannot be assigned to a member with type System. Int32,它是非空值类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26689869/

10-13 00:12