It doesn't really return a single object in the sense you are assuming. What it returns is a "Query", (an object that implements IQueryable<Product>
), that has the capacity to iterate through, and return a set of objects (of type Product
) when the calling code calls foreach
on it. It doesn't actually process the query until the calling code calls foreach
.
You can think of it (the return object) as a set of instructions as to how to process, slice, dice and filter the set of objects you started with (repository.Products
) and generate a new set of objects from that initial set.
The code doesn't actually generate this new set until something calls foreach
on it. If you want this new set to actually be constructed (so you can manipulate it or iterate through it yourself), then you can call ToList()
on it as described by @MotoSV.
List<Product> products = repository.Products
.OrderBy(p => p.ProductID)
.Skip((page - 1) * PageSize)
.Take(PageSize).ToList();