Powiel możliwe:
To return IQueryable<T> or not return IQueryable<T>Dlaczego należy używać IQueryable <T> ponad Lista <T> w LINQ to SQL
mam LINQ do repozytorium SQL realizowany następująco. Metoda GetAll przechwytuje ogólną listę zamiast IQueryable. Jednak w większości przykładów i tutorialów pojawia się okno, które zwraca IQueryable. Jaka jest zaleta przywracania 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();
}
}
}
warstwy biznesowej
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;
}
}
}
CZYTANIE
Powinieneś zobaczyć ten wątek i odpowiedź od Marca Gravell http://stackoverflow.com/questions/718624/to-return-iqueryablet-lub-not-return-iqueryablet – Habib
Prawidłowo, bote to close - 100% duplikatu. – TomTom