0

I'm trying to Convert the Uint(i_Customer) to a Nullable Int(i_Customer) ID Since one is accepting the Null value and the other ID Doesn't support. The parent table is Customer(i_Customer) and the Child is Fault(i_customer). Both, I'm Joining them in a EF Query to get the result. But There is an nullreferenceexception was unhandled by user code Exception that's very disturbing. How Would I fix it?

Here is the EF Query:

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;
            }
Micheal P.
  • 55
  • 11

2 Answers2

0

You need to use DefaultIfEmpty() to provide outer join so that it can handle null values. See example below:

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;
kashi_rock
  • 539
  • 1
  • 5
  • 19
  • Thank you, But I'm Still Getting the same Exception Casted again : `join cInfo in custInfo on s.fault.i_customer equals (int)cInfo.customers.i_Customer into A from cInfo in A.DefaultIfEmpty()` , What do you think it could be? Remember, I'm trying to conver a uint32 into a Nullable Int such as (int?). – Micheal P. Oct 18 '16 at 08:40
  • `(int.)cInfo.customers.i_Customer` Here is the Cause of the Nullable Cast Exception. Could there be an error to pass over it? – Micheal P. Oct 18 '16 at 08:44
  • Can you check if cInfo.customers is null itself? You may also try removing cast from (int)cInfo.customers.i_Customer – kashi_rock Oct 18 '16 at 08:47
  • the `fault.i_customer` which is the child key is (int?) and the parent table `cInfo.customers.i_Customer` key is (Uint) it's not null – Micheal P. Oct 18 '16 at 08:51
0

A normal cast will do

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

Or try this

s.fault.i_customer ?? 0 equals cInfo.customers.i_Customer
Anonymous Duck
  • 2,942
  • 1
  • 12
  • 35