2016-07-18 18 views
6

मैं 2 बाएं जुड़ने की कोशिश कर रहा हूं। मैंने SQL सर्वर में क्वेरी का परीक्षण किया है और यह काम करता है लेकिन मैं linq में क्वेरी को फिर से बनाने में असमर्थ हूं।बाईं ओर से चयन करते समय NullReferenceException

क्वेरी:

select Master.InvoiceId,Consumer.ConsumerId,ConsumerCharge.ChargeId , Amount 
from Master 

left outer join Consumer on 
Master.InvoiceId=Consumer.InvoiceId 

left outer join ConsumerCharge on 
Consumer.ConsumerId = ConsumerCharge.ConsumerId and 
Consumer.InvoiceId = ConsumerCharge.InvoiceId and 
Master.InvoiceId = ConsumerCharge.InvoiceId 

order by InvoiceId 

LINQ में:

var query = from m in IM.GetMaster() 

      join co in CM.GetConsumers() 
      on m.InvoiceId equals co.InvoiceId into temp2 
      from co in temp2.DefaultIfEmpty() 

      join ch in CCM.GetCharge() 
      on new { co.InvoiceId, co.ConsumerId, } equals new { ch.InvoiceId, ch.ConsumerId } into temp 
      from ch in temp.DefaultIfEmpty() 

      orderby m.InvoiceId 
      select new 
      { 
       InvioceID = m.InvoiceId, 
       ConsumerID = co == null ? 0 : co.ConsumerId, 
       ChargeID = ch == null ? 0 : ch.ChargeId, 
       Amount = ch == null ? 0 : ch.Amount 
      }; 

मैं हो रही है

वस्तु संदर्भ एक वस्तु का एक उदाहरण के लिए सेट नहीं।

लाइन on new { co.InvoiceId, co.ConsumerId, } पर। अगर मैं into temp2 from co in temp2.DefaultIfEmpty() हटा देता हूं, तो यह प्रदर्शित करता है लेकिन चालान आईडी जिनके पास कोई उपभोक्ता आईडी प्रदर्शित नहीं होती है। मैं एक उचित बाएं कैसे जुड़ूं जहां 3 टेबल शामिल हैं?

उत्तर

3

left join होने का मतलब है कि अगर कोई दूसरी तालिका में कोई मिलता-जुलता रिकार्ड है तो उन सभी मूल्यों null (एक सामान्य join से अलग है कि यह बाईं मेज से रिकॉर्ड नहीं लौटेगा) कर रहे हैं है.आप co बराबर हो सकता है

var query = from m in IM.GetMaster() 

     join co in CM.GetConsumers() 
     on m.InvoiceId equals co.InvoiceId into temp2 
     from co in temp2.DefaultIfEmpty() 

     join ch in CCM.GetCharge() 
     on new { co?.InvoiceId, co?.ConsumerId, } equals new { ch?.InvoiceId, ch?.ConsumerId } into temp 
     from ch in temp.DefaultIfEmpty() 

     orderby m.InvoiceId 
     select new 
     { 
      InvioceID = m.InvoiceId, 
      ConsumerID = co?.ConsumerId, 
      ChargeID = ch?.ChargeId, 
      Amount = ch?.Amount 
     }; 

इसके अलावा अपने select new

में ?. के उपयोग देखें: कि रिकार्ड के लिए null ताकि आप इसे

जांच करने के लिए प्रयास करें इस राशि

+0

@onedaywhen - मैंने इन वर्गों को दिखाए गए गुणों के साथ बनाया है। लेकिन हाँ .. 'ch' पक्ष में 'बराबर' में 'बराबर' में जोड़ना भूल गया - इसलिए संपादित किया गया :) –

+0

@Scar - क्या यह आपकी समस्या का समाधान करता है? –

+0

यह अब काम करता है, धन्यवाद! :) – Scar

संबंधित मुद्दे