Tuesday, December 1, 2015

ODATA ASP.NET WEB API - $top $skip $orderby $expand $select $filter

OData stands for Open Data Protocol and OData technology gives you the best way to design the Restful service similar asp.net Web API.

OData defines an abstract data model and a protocol that let any client access information exposed by any data source and it allows the creation and consumption of querable and interoperable RESTFULL APIs in simple and standardway.

You can expose data service as OData api for product entity as below manner and can do many things on top of that

  • $top
  • $skip
  • $orderby
  • $expand
  • $select
  • $filter


$top :

A data service URI with a $top System Query Option identifies a subset of the Entries in the Collection of Entries identified by the Resource Path section of the URI. This subset is formed by selecting only the first N items of the set, where N is a positive integer specified by this query option.


The first 5 Product Entries returned where the Collection of Products are sorted using a scheme determined by the OData service.

$skip :

A data service URI with a $skip System Query Option identifies a subset of the Entries in the Collection of Entries identified by the Resource Path section of the URI. That subset is defined by seeking N Entries into the Collection and selecting only the remaining Entries (starting with Entry N+1). N is a positive integer as specified by this query option.


The set of Product Entries (associated with the Category Entry identified by key value 1) starting with the third product.

$orderby :

data service URI with a $orderby System Query Option specifies an expression for determining what values are used to order the collection of Entries identified by the Resource Path section of the URI. This query option is only supported when the resource path identifies a Collection of Entries.


The set of Products is subsequently sorted (in descending order) by the Name property of the related Category Entry.




$expand:

 A URI with a $expand System Query Option indicates that Entries associated with the Entry or   Collection of Entries identified by the Resource Path section of the URI must be represented inline


Identifies the Collection of Categories as well as each of the Products associated with each Category

$select:

A data service URI with a $select System Query Option identifies the same set of Entries as a URI without a $select query option; however, the value of $select specifies that a response from an OData service should return a subset of the Properties which would have been returned had the URI not included a $select query option.

In a response from an OData service, only the Price and Name Property values are returned for each Product Entry within the Collection of products identified.

Querying Data
OData supports various kinds of query options for querying data.

$filter

The $filter allows clients to filter a collection of result that are getting by requested oData URL.

Operator
Description
Example
Logical Operators
Eq
Equal
/Suppliers?$filter=Address/City eq 'Redmond'
Ne
Not equal
/Suppliers?$filter=Address/City ne 'London'
Gt
Greater than
/Products?$filter=Price gt 20
Ge
Greater than or equal
/Products?$filter=Price ge 10
Lt
Less than
/Products?$filter=Price lt 20
Le
Less than or equal
/Products?$filter=Price le 100
And
Logical and
/Products?$filter=Price le 200 and Price gt 3.5
Or
Logical or
/Products?$filter=Price le 3.5 or Price gt 200
Not
Logical negation
/Products?$filter=not endswith(Description,'milk')
Arithmetic Operators
Add
Addition
/Products?$filter=Price add 5 gt 10
Sub
Subtraction
/Products?$filter=Price sub 5 gt 10
Mul
Multiplication
/Products?$filter=Price mul 2 gt 2000
Div
Division
/Products?$filter=Price div 2 gt 4
Mod
Modulo
/Products?$filter=Price mod 2 eq 0
Grouping Operators
( )
Precedence grouping
/Products?$filter=(Price sub 5) gt 10


Other Source :


No comments:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...