2011-05-24 9 views
11

मैं LINQ में क्वेरी के नीचे लिखा है बाईं प्रदर्शन करने के लिए शामिल हो लेकिन इसकी फेंक त्रुटि:त्रुटि शामिल हों

var qry = from c in dc.category_feature_Name_trans_SelectAll_Active() 
      join p in dc.product_category_feature_trans_SelectAll() 
      on c.cft_id equals p.cft_id into cp 
      from p in cp.DefaultIfEmpty()      
      select new 
      { 
       c.cft_id, 
       c.feature_id, 
       c.feature_name, 
       p.product_id , 
       p.value 
      }; 

त्रुटि:

Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the 
current web request. Please review the stack trace for more information about 
the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 

Source Error: 

Line 57:      on c.cft_id equals p.cft_id into cp 
Line 58:      from p in cp.DefaultIfEmpty()      
error Line 59:      select new 
Line 60:      { 
Line 61:       c.cft_id, 

कृपया मेरी मदद करो।

उत्तर

13

cp.DefaultIfEmpty() एक दृश्य जो एक शून्य मान में अगर cp खाली था होगा देता है।

इसका मतलब है कि आप इस तथ्य p में

from p in cp.DefaultIfEmpty() 

अशक्त हो सकता है कि के लिए खाते में करने के लिए है। अब, आपने वास्तव में यह नहीं कहा है कि आप उस मामले में क्या करना चाहते हैं। आपको ऐसा कुछ चाहिए:

var qry = from c in dc.category_feature_Name_trans_SelectAll_Active() 
      join p in dc.product_category_feature_trans_SelectAll() 
      on c.cft_id equals p.cft_id into cp 
      from p in cp.DefaultIfEmpty()      
      select new 
      { 
       c.cft_id, 
       c.feature_id, 
       c.feature_name, 
       product_id = p == null ? null : p.product_id, 
       value = p == null ? null : p.value 
      }; 

... या आप कुछ अलग हैंडलिंग चाहते हैं। हम p.product_id या p.value के प्रकारों को नहीं जानते हैं, जो मदद नहीं करते हैं। (उदाहरण के लिए, यदि उपरोक्त कोड के साथ आपको थोड़ा और काम चाहिए तो product_id एक मान प्रकार है।)

+0

'product_id = p == शून्य? null: p.product_id' संकलित नहीं होगा, अगर product_id int –

+1

@Alex है: यही कारण है कि मैंने अंतिम वाक्य लिखा था ... यदि product_id' स्ट्रिंग है तो आपका संस्करण संकलित नहीं होगा :) –

+1

कोई त्रुटि नहीं ... सामान्य रूप से जॉन हमेशा उचित स्पष्टीकरण के साथ सही उत्तर के साथ आता है। धन्यवाद जॉन –

9

आप बाएं शामिल हो रहे हैं, इसलिए pnull हो सकता है। इसके लिए आपको खाते की आवश्यकता है।

यहां प्रश्न है जो काम करना चाहिए, हालांकि मुझे यह सुनिश्चित नहीं है कि p.value किस प्रकार का मान है। यदि क्वेरी एक संदर्भ प्रकार है तो क्वेरी काम करेगी। यदि मान मूल्य प्रकार है, तो product_id कास्ट के समान कास्ट का उपयोग करने के बजाय।

var qry = from c in dc.category_feature_Name_trans_SelectAll_Active() 
      join p in dc.product_category_feature_trans_SelectAll() 
      on c.cft_id equals p.cft_id into cp 
      from p in cp.DefaultIfEmpty() 
      select new 
      { 
       c.cft_id, 
       c.feature_id, 
       c.feature_name, 
       product_id = p == null ? (int?)null : p.product_id, 
       value = p == null ? null : p.value, 
      }; 
+0

धन्यवाद यह मेरे लिए काम करता है .. –

0

डिफ़ॉल्ट मॉडल फ़ाइल() फ़ंक्शन के लिए पैरामीटर के रूप में अपने मॉडल क्लास का उपयोग करें।

from p in cp.DefaultIfEmpty(new yourModelClass())