I have this method, it selects data in a particular range (pageIndex and pageSize)
public PagedList(IQueryable<T> source, int pageIndex, int pageSize)
{
this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
}
I want to create an overloading method which selects all data, so, here’s my code
public PagedList(IQueryable<T> source)
{
//this.AddRange(source.Select(x => new T()).ToList()); (1)
this.AddRange(source.AsQueryable().ToList()); (2)
}
Firstly, I tried (1), but it didn’t accept T. Then I tried (2), and it’s recommended that I should make parameter type INumerable instead of IQueryable. What is the solution to select all data in this case?
Thanks
You can do it simply like this:
IEnumerable<T>as parameter type instead ofIQueryable<T>, because you don’t use any features specific toIQueryable<T>.AsQueryablebecause you simply want all dataToListasList<T>.AddRangeinternally already performs a copy. WithToListthere would be two copy operations going on.