Skip to content

Getting started with Simple.OData.Client

Jason Finch edited this page Aug 23, 2018 · 37 revisions

The easiest way to start using Simple.OData.Client is to install it’s Nuget package. In Visual Studio open Package Manager console and type the following:

Install-Package Simple.OData.Client

You will see output similar to this:

Successfully installed 'Simple.OData.Client 5.4.0'.
Successfully added 'Simple.OData.Client 5.4.0' to SimpleODataTest.

In the source file where you will be using OData provider import namespaces:

using Simple.OData.Client;

Create an instance of ODataClient by passing an OData service URL:

var client = new ODataClient("http://packages.nuget.org/v1/FeedService.svc/");

Now you can access data from the OData server using either basic or fluent API.
Example of basic API syntax:

var packages = await client
    .FindEntriesAsync("Packages?$filter=Title eq 'Simple.OData.Client'");
foreach (var package in packages)
{
    Console.WriteLine(package["Title"]);
}

Example of dynamic fluent API syntax:

var x = ODataDynamic.Expression;
IEnumerable<dynamic> packages = await client
    .For(x.Packages)
    .Filter(x.Title == "Simple.OData.Client")
    .FindEntriesAsync();
foreach (var package in packages)
{
    Console.WriteLine(package.Title);
}

Example of typed fluent API syntax (assuming there is a class Package defined):

var packages = await client
    .For<Package>()
    .Filter(x => x.Title == "Simple.OData.Client")
    .FindEntriesAsync();
foreach (var package in packages)
{
    Console.WriteLine(package.Title);
}

Note that the examples above refer to the OData collection as “Package” or “Packages”. Simple.OData.Client has a built-in English word pluralizer that can handle both singular and plural forms.


See also:
Simple.OData.Client basic API
Simple.OData.Client fluent API
Retrieving data
Modifying data