WCF Tutorial
www.Learn2Expert.net A new ASP.Net MVC 4, SSIS, Interview Q/A tutorial - Visit - www.Learn2Expert.net
Skip Navigation LinksHomeOData Expression No of Views: 25487

OData Expression

This section lets understand following set of operation in resource by applying query in url

  1. To return the resource in different format
  2. Filter the records
  3. Read only the required fields
  4. Pagination
  5. Ordering the records

All the query URL mentions below are applied on top of Service create in above article. Refer "Create OData Service" section.

URL: http://localhost:24641/MyODataService.svc/People()

Comments: Return all the resource available in "People" property in AtomPub(feeds) format (default)

Sample:

URL: http://localhost:24641/MyODataService.svc/People()?$format=json

Comments: Return the resource in JSON format

Sample:

URL: http://localhost:24641/MyODataService.svc/People(5)

Comments: Return only the "Person" with business ID (primary key) = "5"

Sample:

URL: http://localhost:24641/MyODataService.svc/People(5)?$select=BusinessEntityID,FirstName,LastName

Comments: Returns only the selected fields from select option

Sample:

URL: http://localhost:24641/MyODataService.svc/People()?$skip=20&$top=3&$inlinecount=allpages

Comments: This url returns only top 3 by skipping the first 20 records and on "allpages" means that query is applied on all page

Note:

Consider scenario when return values of the query is very large amount of data. Does system return all value?

No, system will return only the set of records which is mention in the service setting (Max number of records). System automatically applies the Pagination to give only set of records. Resource for the next set of records are access by URL provide by system at end of First set of records

  • $top – specifies how many results should be returned in the current response
  • $skip – specifies how many records that satisfy the current criterion should be skipped
  • $skiptoken – specifies that all results from the start to the some id should be skipped
  • $inlinecount – if it is set to allpages requires that total number of records should be returned.

Sample:

URL: http://localhost:24641/MyODataService.svc/People()?$orderby=BusinessEntityID%20desc

Comments: return the records in ascending or descending order

Sample:

Tips!

  • Always create the service with Interface->Implementation format, mention the contract in Interface.
  • Define the service in Class library and refer the class library in Host project. Don’t use service class in host project.
  • Change the instance mode to per call as default.
  • Always catch exception using try/catch block and throw exception using FaultException < T >.
  • Logging and Include exception should be enable while compiling the project in debug mode. While in production deployment disable the logging and Include exception details.