Why should I use IQueryable <T> over List <T> in LINQ to SQL

Possible duplicate:
To return an IQueryable <T> or not to return an IQueryable <T>

I have a LINQ to SQL repository implemented as follows. The GetAll method saves the general list instead of IQueryable. However, in most examples and tutorials, the pit, as shown, returns IQueryable. What is the advantage of maintaining 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 level

 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

+6
source share
3 answers

Using IQueryable , the LINQ to move some additional work in the database, creating a variety of SQL-queries. eg. when you try something like GetAll().Where(condition) and use List , all elements are requested from the database and where the condition is checked on the application side. When you use IQueryable , it can be transferred to the database, and the proper elements are returned right from there.

+4
source

IQueryable extends IEnumerable . Both of them do not project / accumulate their data until they are repeated, while IList objects pull out all their data and fill in when assigned.

Thus, it is a "lazy load" and the distinction is "impatient."

+2
source

Becasue IList - ah - not smart?

Here we go:

IIF yo uexport IQueryable - in the Get method, this is the only method you will ever need. All parameters are included in IQueryable and BECAUSE OF DELAYED EXECUTION END UP IN YOUR SQL LAYER.

Export IList, and you will get ALL and THEN filtering memory, which is like a perversion of LINQ as it is received.

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

+1
source

Source: https://habr.com/ru/post/918634/


All Articles