2012-12-13 22 views
15

मैं इस विधि चीजों की एक सूची प्राप्त करने की कोशिश करता है:रिटर्निंग खाली IQueryable <>

private static IQueryable<Thing> GetThings(int thingsType) 
     { 
       try 
       { 
        return from thing in entities.thing.Include("thingStuff") 
          select thing; 
       } 
       catch (Exception exception) 
       { 
        return new EnumerableQuery<Thing>(?????); 
       } 
      } 

     } 

मैं एक खाली IQueryable वापस जाने के लिए अगर मैं क्वेरी चलाने के लिए किसी भी कारण से नहीं मिल सकता है चाहता हूँ। मैं न्यूल वापस नहीं करना चाहता क्योंकि यह कॉलिंग कोड तोड़ सकता है। क्या यह संभव है या मैं इस बारे में पूरी तरह गलत हूं?

उत्तर

33

ये जवाब अच्छे होते हैं और काम करते हैं, फिर भी मैं हमेशा खाली का उपयोग कर और एक नया सूची नहीं बना रहा है महसूस किया है क्लीनर:

Enumerable.Empty<Thing>().AsQueryable(); 
+0

यह एक अच्छा जवाब है। – Irwin

+1

लेकिन ध्यान रखें कि यदि आप ईएफ 6 के साथ प्रतीक्षा/एसिंक का उपयोग करते हैं तो आप निम्नलिखित अपवाद के साथ समाप्त हो जाएंगे: https://msdn.microsoft.com/en-us/data/dn313107.aspx - इस लिंक को async तरीके से जांचें - http://stackoverflow.com/questions/33305495/how-to-return-empty-iqueryable-in-an-async-repository- विधि – jabko87

9

निम्नलिखित का प्रयास करें:

private static IQueryable<Thing> GetThings(int thingsType) 
    { 
      IQueryable<Thing> things = new List<Thing>().AsQueryable(); 
      try 
      { 
       things = from thing in entities.thing.Include("thingStuff") 
         select thing; 

       return things; 
      } 
      catch (Exception exception) 
      { 
       return things; 
      } 
     } 
+0

यह आशाजनक प्रतीत होता है, मैं इसे आज़मा दूंगा, धन्यवाद! –

+0

@mreyeros यदि आप कोई अपवाद नहीं फेंकते हैं तो आप कभी भी कुछ भी वापस नहीं करते हैं। शायद आप अंत में अपनी वापसी का मतलब था? – cadrell0

5

मैं ब्लॉक finally {} जोड़ सकते हैं और है कि कोड में मेरी वापसी प्रकार जाते थे।

यह आपके आवेदन की अपेक्षा के प्रकार को वापस कर इस मुद्दे का ख्याल रखेगा।

private static IQueryable<T> GetThings(int thingsType) 
    { 
      IQueryable<T> list = new List<Thing>().AsQueryable(); 
      try 
      { 
       list = from thing in entities.thing.Include("thingStuff") 
         select t; 
      } 
      catch (Exception exception) 
      { 
       // handle exception here; 
      } 
      finally {  
       return list; 
      } 

     } 

    } 
+2

निश्चित रूप से सहमत हैं, कुछ फैशन या किसी अन्य में अपवाद को संभाला जाना चाहिए, और फिर खाली सूची को वापस करने के लिए कॉल अंततः ब्लॉक में वापस किया जाना चाहिए। – mreyeros

+0

धन्यवाद, यह कोडिंग की थोड़ी अधिक रक्षात्मक शैली है :) –

1

मुझे लगता है कि इस tidier होगा:

private static IQueryable<T> GetThings(int thingsType) 
{ 
    try 
    { 
     return from thing in entities.thing.Include("thingStuff") 
        select t; 
    } 
    catch (Exception exception) 
    { 
     // Exception handling code goes here 

     return new List<Thing>().AsQueryable(); 
    } 
} 
1

खाली लौटने योग्य IQueryable <> डीबीसेट। टेक (0)

+0

मा आप उत्तर के लिए एक स्पष्टीकरण जोड़ते हैं? –

+1

जबकि आपने इस उपयोगकर्ता की समस्या का समाधान किया हो सकता है, तो कोड-केवल उत्तर भविष्य में इस प्रश्न पर आने वाले उपयोगकर्ताओं के लिए बहुत उपयोगी नहीं हैं। कृपया अपना उत्तर दें कि यह समझाने के लिए कि आपका कोड मूल समस्या क्यों हल करता है। –

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