-
Notifications
You must be signed in to change notification settings - Fork 200
Results projection, paging and ordering
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:
Request 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:
Request 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:
Request 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:
Request 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:
Request 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:
Request 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:
Request 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:
Retrieving data
Simple.Data documentation for Select
Simple.Data documentation for Order