【问题标题】:Ho to Convert a Uint to Nullable Int in a EF Query何在 EF 查询中将 Uint 转换为 Nullable Int
【发布时间】:2016-10-18 08:14:09
【问题描述】:

我正在尝试将 Uint(i_Customer) 转换为 Nullable Int(i_Customer) ID,因为一个接受 Null 值而另一个 ID 不支持。 父表是 Customer(i_Customer),子表是 Fault(i_customer)。两者,我都在 EF 查询中加入它们以获得结果。但是有一个非常令人不安的nullreferenceexception was unhandled by user code 异常。我该如何解决?

这是 EF 查询:

if (servicelevel == 3)
            {
                result = (from s in res
                          join cInfo in custInfo on 
                          s.fault.i_customer equals Convert.ToInt32((int?)cInfo.customers.i_Customer) 
                          where (s.fault.resolved == null) || (s.tasks.assignedto == agent)
                          orderby s.fault.ispriority descending, s.fault.logtime ascending
                          select new ActiveFaultResult()
                          {   Company_Name = cInfo.customers.Company_Name,
                                  //replies = replies,
                                  idFaults = s.fault.idFaults,
                                  hashvalue = s.fault.hashvalue,
                                  responsible = s.fault.responsible,
                                  logtime = s.fault.logtime,
                                  isinternal = s.fault.isinternal,
                                  ispriority = s.fault.ispriority

                           }).ToList<ActiveFaultResult>();

                //  var limitresult = result.Take(50);
                return result;
            }

【问题讨论】:

  • 为什么不将您的 i_Customer 定义为 int? ?
  • @mybirthname,这可以定义它并在 EF 查询中使用它吗?如果是这样,如何?请帮忙。
  • Convert.ToInt32((int?)cInfo.customers.i_Customer) => (int?)cInfo.customers.i_Customer

标签: c# mysql entity-framework linq visual-studio-2015


【解决方案1】:

普通演员就可以了

s.fault.i_customer equals (int?)cInfo.customers.i_Customer

或者试试这个

s.fault.i_customer ?? 0 equals cInfo.customers.i_Customer

【讨论】:

  • 太棒了。我刚刚弄明白了:join cust in custInfo on Convert.ToUInt32(f.i_customer ?? 0) equals cust.i_customer into custo from custlis in custo.DefaultIfEmpty() 只是通过您的代码。再次感谢
  • @MichealP。很高兴它有帮助
  • 谢谢,我只是转换了错误的 ID。非常感谢先生!
【解决方案2】:

您需要使用 DefaultIfEmpty() 来提供外连接,以便它可以处理空值。请参见下面的示例:

result = (from s in res
         join cInfo in custInfo on s.fault.i_customer equals (int)cInfo.customers.i_Customer into A
         from cInfo in A.DefaultIfEmpty()
         where (s.fault.resolved == null) || (s.tasks.assignedto == agent)
         orderby s.fault.ispriority descending, s.fault.logtime ascending
         select new ActiveFaultResult()
         {
                Company_Name = cInfo == null? String.Empty: cInfo.customers.Company_Name,
                idFaults = s.fault.idFaults,
                hashvalue = s.fault.hashvalue,
                responsible = s.fault.responsible,
                logtime = s.fault.logtime,
                isinternal = s.fault.isinternal,
                ispriority = s.fault.ispriority
         }).ToList<ActiveFaultResult>();
return result;

【讨论】:

  • 谢谢,但我仍然遇到同样的异常:join cInfo in custInfo on s.fault.i_customer equals (int)cInfo.customers.i_Customer into A from cInfo in A.DefaultIfEmpty(),你认为它可能是什么?请记住,我正在尝试将 uint32 转换为 Nullable Int,例如 (int?)。
  • (int.)cInfo.customers.i_Customer 这是 Nullable Cast 异常的原因。会不会有错误可以跳过?
  • 你能检查一下 cInfo.customers 本身是否为空吗?您也可以尝试从 (int)cInfo.customers.i_Customer 中移除演员表
  • 子键 fault.i_customer 是 (int?) 而父表 cInfo.customers.i_Customer 键是 (Uint) 它不为空
猜你喜欢
  • 1970-01-01
  • 2014-07-31
  • 2012-08-27
  • 2020-01-25
  • 2017-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多