8

Possible Duplicate:
To return IQueryable<T> or not return IQueryable<T>

I have LINQ to SQL repository implemented as follows. The GetAll method is returing a generic List instead of IQueryable. However in most of the examples and tutorials pit is shown to return IQueryable. What is the advantage of returing 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();
    }


  }
}

Business Layer

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;
    }


 }
}

READING

  1. Advantage of creating a generic repository vs. specific repository for each object?
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • 1
    You should see this thread and answer by Marc Gravell http://stackoverflow.com/questions/718624/to-return-iqueryablet-or-not-return-iqueryablet – Habib Jun 21 '12 at 06:26
  • 1
    Correct, bote to close - 100% duplicate. – TomTom Jun 21 '12 at 06:28

3 Answers3

6

Using IQueryable let LINQ move some additional work into DB by creating different SQL queries. eg. when you try something like GetAll().Where(condition) and use List all items are queried from DB and where condition is checked application-side. When you use IQueryable it can be moved to DB and proper items are returner directly from there.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • 1
    Another answer that can complement this answer is: http://stackoverflow.com/a/2876655/170386 –  Jun 21 '12 at 06:32
3

IQueryable extends IEnumerable. Both do not project/inflate their data until being iterated, whereas IList objects pull all their data and are populated when assigned to.

So it's a "lazy-load" vs. "eager-load" distinction.

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
  • 1
    Thanks for the information. IQueryable is better than IEnumerable. IEnumerable will execute the original query in the database, then filtering out in the memory. http://stackoverflow.com/questions/2876616/returning-ienumerablet-vs-iqueryablet/2876655#2876655 – LCJ Jun 21 '12 at 06:40
1

Becasue IList is - ah - not smart?

Here we go:

IIF yo uexport IQueryable - on the Get method it is the only method you ever need. All parameters go into the IQueryable and BECAUSE OF DELAYED EXECUTION END UP IN YOUR SQL LAYER.

Export IList and you GET ALL and THEN filter - in memory, which is as much a perversion of LINQ as it gets.

The real trick here is that if I makcal your Get method and then .Where, OrderBy, that gets into the sql statement in the database.

TomTom
  • 61,059
  • 10
  • 88
  • 148