2016-10-18 9 views
0

Uint(i_Customer)をNullable Int(i_Customer)IDに変換しようとしています.Null値を受け入れていて、他のIDがサポートしていないためです。 親テーブルはCustomer(i_Customer)で、ChildはFault(i_customer)です。どちらも、結果を得るためにEF Queryに参加しています。しかし、非常に混乱しているnullreferenceexception was unhandled by user code例外があります。どうすれば修正できますか?ここでHoがEFクエリでNintable IntにNintable Intを変換する

は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; 
      } 
+0

を試してみてください? ? – mybirthname

+0

@mybirthname、これを定義してEFクエリで使用することは可能でしょうか?もしそうなら、どうですか?手伝ってください。 –

+0

Convert.ToInt32((int?)cInfo.customers.i_Customer)=>(int?)cInfo.customers.i_Customer –

答えて

0

を通常のキャストは

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

を行いますかなぜintとしてごi_Customerを定義されていないこの

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

これは素晴らしいものです。私はちょうどそれを把握しています: 'あなたのコードだけでConvert.ToUInt32(f.i_customer ?? 0)のcustInfoをcustoに追加すると、custo.DefaultIfEmpty()のcustlisからcusto がcustoになります。再度ありがとう –

+0

@MichealP。それがうれしいです – Sherlock

+0

ありがとう、私はちょうど間違ったIDを変換していた。本当にそれを感謝するサー! –

0

あなたはそれがnull値を扱うことができるように外部結合を提供するために、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; 
+0

ありがとう、しかし、私はまだ同じ例外を取得しています。 fault.i_customerは、A.DefaultIfEmpty()のcInfoからA にcInfo.customers.i_Customerと等しいです。それはどういうことでしょうか?覚えておいて、私はuint32を(int?)のようなNullable Intに変換しようとしています。 –

+0

'(int。)cInfo.customers.i_Customer'ここにNullableキャスト例外の原因があります。それを越えるためにエラーがありますか? –

+0

cInfo.customersがnullであるかどうかを確認できますか? cInfo.customers.i_Customer –