Skip to content

Results projection, paging and ordering

object edited this page Oct 9, 2012 · 7 revisions

Simple.Data OData adapter supports all stanard OData query modifiers that can be used to control number of rows and columns fetched during the request execution.


Retrieve ProductID for all products

IEnumerable<dynamic> products = _db.Products.All()
    .Select(_db.Products.ProductID);
Assert.True(products.First().ProductID > 0);
Assert.Throws<RuntimeBinderException>(() => products.First().ProductName);

Request URI: GET Products?$select=ProductID


Retrieve only the first row from the Products collection

IEnumerable<dynamic> products = _db.Products.All()
    .Take(1);
Assert.Equal(1, products.Count());

Request URI: GET Products?$top=1


Retrieve all except the first row from the Products collection

IEnumerable<dynamic> products = _db.Products.All()
    .Skip(1);
Assert.Equal(76, products.Count());

Request URI: GET Products?$skip=1


Skip two and retrieve one row from the Products collection

IEnumerable<dynamic> products = _db.Products.All()
    .Skip(2)
    .Take(1);
Assert.Equal(1, products.Count());

Request URI: GET Products?$skip=2&$top=1


Retrieve all products ordered by product name

IEnumerable<dynamic> products = _db.Products.All()
    .OrderBy(_db.Products.ProductName);
Assert.Equal("Alice Mutton", products.First().ProductName);

Request URI: GET Products?$orderby=ProductName


Retrieve all products ordered descending by product name

IEnumerable<dynamic> products = _db.Products.All()
    .OrderByDescending(_db.Products.ProductName);
Assert.Equal("Zaanse koeken", products.First().ProductName);

Request URI: GET Products?$orderby=ProductName%20desc


Retrieve product names in descending order

IEnumerable<dynamic> products = _db.Products.All()
    .OrderByDescending(_db.Products.ProductName)
    .Select(_db.Products.ProductName);
Assert.Equal("Zaanse koeken", products.First().ProductName);

Request URI: GET Products?$orderby=ProductName%20desc&$select=ProductName


See also:
Retrieving data
Simple.Data documentation for Select
Simple.Data documentation for Order