Skip to content

Results projection, paging and ordering

object edited this page Oct 5, 2012 · 13 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:
OData URI: Products?$select=ProductID

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

Retrieve only the first row from the Products collection:
OData URI: Products?$top=1

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

Retrieve all except the first row from the Products collection:
OData URI: Products?$skip=1

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

Skip two and retrieve one row from the Products collection:
OData URI: Products?$skip=2&$top=1

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

Retrieve all products ordered by product name:
OData URI: Products?$orderby=ProductName

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

Retrieve all products ordered descending by product name:
OData URI: Products?$orderby=ProductName%20desc

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

Retrieve product names in descending order:
OData URI: Products?$orderby=ProductName%20desc&$select=ProductName

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

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