LINQ错误:null值不能分配给类型System.Int32一个成员是一个非空的值类型 [英] LINQ error: The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type

查看:482
本文介绍了LINQ错误:null值不能分配给类型System.Int32一个成员是一个非空的值类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询:

  VAR的查询=(从table1.Where T1(C => c.ServiceID == 2及和放大器; c.IsDeleted == 1)
             从在table2.Where T2(℃= GT; c.RelServiceBonID == t1.RelServiceBonID&放大器;&放大器; c.IsDeleted == 1).DefaultIfEmpty()
             从在table3.Where T3(℃= GT; c.BonID == t1.BonID&放大器;&放大器; c.UserID == 8和;&放大器; c.IsDeleted == 1).DefaultIfEmpty()
             从在table4.Where T4(C => c.BonID == t1.BonID&放大器;&放大器; c.IsDeleted == 1)
             新选择
             {
                 t3.UserID,t2.SubServiceID,
             });query.Dump();

当我跑,我得到了以下错误:


  

空值不能分配给类型System.Int32一个成员是一个非空的值类型。


用户名字段是主键,我不能设置为可为空!

我如何传递这个问题?


解决方案

  

的用户ID字段是主键,我不能设置为可为空


由于HVD下面解释,因为你有有效的外部联接,用户名仍然可以为null。除此之外, SubServiceID 也为空。通常它有助于罪魁祸首转换为可空类型:

 新选择
 {
     用户名=(INT?)t3.UserID,
     SubServiceID =(INT?)t2.SubServiceID
 });

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?

解决方案

The UserID field is primary key and I can not set that as nullable

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:

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

这篇关于LINQ错误:null值不能分配给类型System.Int32一个成员是一个非空的值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆