Domanda

What kind of object this line returns ? How can I see the metadata of it to see the inheritance and methods which has ?

IQueryable<Product> products = repository.Products
                        .OrderBy(p => p.ProductID)
                        .Skip((page - 1) * PageSize)
                        .Take(PageSize);

In debug mode I see it returns the following object:

System.Collections.Generic.List`1[SportsStore.Domain.Entities.Product].OrderBy(p => p.ProductID).Skip(3).Take(3)
È stato utile?

Soluzione

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();

Altri suggerimenti

It returns an IQueryable of type Products to a collection of Product instances. If you want to see the contents of products, then put a breakpoint on the LINQ statement, step over that line and then hover over the products variable to see it's contents.

Note, you may have to do a ToList() at the end of the LINQ statement in order to see the results as they may not yet be returned from your repository. Check if the Take methods "eager" loads the results. If your repository is a collection of objects, then a ToList won't be required, but if you are accessing a database, i.e. through Entity Framework, then it will be.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top