2012-06-21 11 views
6

संभव डुप्लिकेट SQL का उपयोग करना चाहिए:
To return IQueryable<T> or not return IQueryable<T>मैं क्यों IQueryable <T> सूची <T> से अधिक LINQ में

मैं इस प्रकार के रूप में लागू एसक्यूएल भंडार करने के लिए LINQ की है। GetAll विधि IQueryable की बजाय एक सामान्य सूची को पुनर्प्राप्त कर रहा है। हालांकि अधिकांश उदाहरणों और ट्यूटोरियल पिट में IQueryable लौटने के लिए दिखाया गया है। IQueryable को पुनर्प्राप्त करने का क्या फायदा है?

using System.Linq; 
namespace RepositoryLayer 
{ 
public interface IRepository<T> where T : class 
{ 
    //System.Linq.IQueryable<T> GetAll(); 
    System.Collections.Generic.List<T> GetAll(); 
} 

public class Repository<T> : IRepository<T> where T : class 
{ 
    public System.Data.Linq.DataContext Context 
    { 
     get; 
     set; 
    } 

    //public virtual System.Linq.IQueryable<T> GetAll() 
    //{ 
    // //GetAll is returning generic Queryable<T>. 
    // System.Linq.IQueryable<T> allItems = Context.GetTable<T>(); 
    // return allItems; 
    //} 

    public virtual System.Collections.Generic.List<T> GetAll() 
    { 

     System.Linq.IQueryable<T> allItems = Context.GetTable<T>(); 
     return allItems.ToList(); 
    } 


    } 
} 

व्यापार लेयर

namespace BusinessLayerProject 
{ 
public class AccountBusiness 
{ 
    //IRepository<T> 
    RepositoryLayer.IRepository<RepositoryLayer.Account> accountRepository; 
    public AccountBusiness(RepositoryLayer.IRepository<RepositoryLayer.Account> repo) 
    { 
     accountRepository = repo; 
    } 

    //public List<RepositoryLayer.Account> GetAllAccounts() 
    //{ 

    // //LibraryManagementClassesDataContext context = new LibraryManagementClassesDataContext(); 
    // //List<RepositoryLayer.Account> accontList = context.Accounts.ToList(); 

    // System.Linq.IQueryable<RepositoryLayer.Account> acc = accountRepository.GetAll(); 
    // return acc.ToList(); 
    //} 

    public List<RepositoryLayer.Account> GetAllAccounts() 
    { 
     List<RepositoryLayer.Account> acc = accountRepository.GetAll(); 
     return acc; 
    } 


} 
} 

  1. Advantage of creating a generic repository vs. specific repository for each object?
+1

आपको यह धागा देखना चाहिए और मार्क ग्रेवल द्वारा जवाब देना चाहिए http://stackoverflow.com/questions/718624/to-return-iqueryablet-or-not-return-iqueryablet – Habib

+1

सही, बंद करने के लिए बोतलें - 100% डुप्लिकेट। – TomTom

उत्तर

4

पढ़ना का उपयोग IQueryable LINQ विभिन्न एसक्यूएल प्रश्नों बनाने के द्वारा डीबी में कुछ अतिरिक्त काम बढ़ते हैं। जैसे। जब आप GetAll().Where(condition) जैसे कुछ कोशिश करते हैं और List का उपयोग करते हैं तो सभी आइटम डीबी से पूछे जाते हैं और जहां स्थिति की जांच की जाती है। जब आप IQueryable का उपयोग करते हैं तो इसे डीबी में ले जाया जा सकता है और उचित आइटम सीधे वहां से रिटर्नर होते हैं।

+1

एक और उत्तर जो इस उत्तर का पूरक हो सकता है: http://stackoverflow.com/a/2876655/170386 –

2

IQueryableIEnumerable फैलाता है। दोनों पुनरावृत्त होने तक अपने डेटा को प्रोजेक्ट/फुलाते नहीं हैं, जबकि IList ऑब्जेक्ट्स अपने सभी डेटा खींचते हैं और आवंटित होने पर पॉप्युलेट किए जाते हैं।

तो यह एक "आलसी लोड" बनाम "उत्सुक-भार" भेद है।

+1

जानकारी के लिए धन्यवाद। IQueryable IENumerable से बेहतर है। IENumerable डेटाबेस में मूल क्वेरी निष्पादित करेगा, फिर स्मृति में फ़िल्टरिंग करेगा। http://stackoverflow.com/questions/2876616/returning-ienumerablet-vs-iqueryablet/2876655#2876655 – Lijo

1

Becasue IList है - आह - स्मार्ट नहीं है?

ये हम चले:

IIF यो IQueryable uexport - प्राप्त पद्धति पर यह एकमात्र तरीका क्या तुमने कभी की जरूरत है। सभी पैरामीटर IQueryable में जाते हैं और आपके SQL LAYER में समाप्त किए गए निष्पादन अंत के कारण।

निर्यात IList और आपको सभी और फिर फ़िल्टर प्राप्त करें - स्मृति में, जो कि LINQ के जितना अधिक विकृति है उतना ही हो जाता है।

असली चाल यह है कि यदि मैं आपकी गेट विधि बनाती हूं और फिर। जहां, ऑर्डरबी, जो डेटाबेस में SQL स्टेटमेंट में आता है।

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