Duration (Pacific Standard Time):
To (Pacific Standard Time):
Impact:
  • None
User Action:
  • None
Visual Studio 2012 - Visual F#

Walkthrough: Accessing an OData Service by Using Type Providers (F#)

OData, meaning Open Data Protocol, is a protocol for transferring data over the Internet. Many data providers expose access to their data by publishing an OData web service. You can access data from any OData source in F# 3.0 using data types that are automatically generated by the ODataService type provider. For more information about OData, see Introducing OData.

This walkthrough shows you how to use the F# ODataService type provider to generate client types for an OData service and query data feeds that the service provides.

This walkthrough illustrates the following tasks, which you should perform in this order for the walkthrough to succeed:

In this step, you set up a project to use an OData type provider.

To configure a client project for an OData service

  • Open an F# Console Application project, and then add a reference to the System.Data.Services.Client Framework assembly.

  • Under Extensions, add a reference to the FSharp.Data.TypeProviders assembly.

In this step, you create a type provider that provides access to the types and data for an OData service.

To access OData types

  • In the Code Editor, open an F# source file, and enter the following code.

    No code example is currently available or this language may not be supported.
    

    In this example, you have invoked the F# type provider and instructed it to create a set of types that are based on the OData URI that you specified. Two objects are available that contain information about the data; one is a simplified data context, db in the example. This object contains only the data types that are associated with the database, which include types for tables or feeds. The other object, fullContext in this example, is an instance of DataContext and contains many additional properties, methods, and events.

In this step, you use F# query expressions to query the OData service.

To query an OData service

  1. Now that you've set up the type provider, you can query an OData service.

    OData supports only a subset of the available query operations. The following operations and their corresponding keywords are supported:

    • projection (select)

    • filtering (where, by using string and date operations)

    • paging (skip, take)

    • ordering (orderBy, thenBy)

    • AddQueryOption and Expand, which are OData-specific operations

    For more information, see LINQ Considerations (WCF Data Services).

    If you want all of the entries in a feed or table, use the simplest form of the query expression, as in the following code:

    No code example is currently available or this language may not be supported.
    
  2. Specify the fields or columns that you want by using a tuple after the select keyword.

    No code example is currently available or this language may not be supported.
    
  3. Specify conditions by using a where clause.

    No code example is currently available or this language may not be supported.
    
  4. Specify a substring condition to the query by using the Contains method. The following query returns all products that have "Chef" in their names. Also notice the use of GetValueOrDefault. The UnitPrice is a nullable value, so you must either get the value by using the Value property, or you must call GetValueOrDefault.

    No code example is currently available or this language may not be supported.
    
  5. Use the EndsWith method to specify that a string ends with a certain substring.

    No code example is currently available or this language may not be supported.
    
  6. Combine conditions in a where clause by using the && operator.

    No code example is currently available or this language may not be supported.
    

    The operators ?> and ?< are nullable operators. You can use a full set of nullable equality and comparison operators. For more information, see Linq.NullableOperators Module (F#).

  7. Use the sortBy query operator to specify ordering, and use thenBy to specify another level of ordering. Notice also the use of a tuple in the select part of the query. Therefore, the query has a tuple as an element type.

    No code example is currently available or this language may not be supported.
    
  8. Ignore a specified number of records by using the skip operator, and use the take operator to specify a number of records to return. In this way, you can implement paging on data feeds.

    No code example is currently available or this language may not be supported.
    

Every OData query is translated into a specific OData request URI. You can verify that URI, perhaps for debugging purposes, by adding an event handler to the SendingRequest event on the full data context object.

To verify the OData request

  • To verify the OData request URI, use the following code:

    No code example is currently available or this language may not be supported.
    

    The output of the previous code is:

    requesting http://services.odata.org/Northwind/Northwind.svc/Orders()?$orderby=ShippedDate&$select=OrderID,ShippedDate