C# – ODATA Consume Service Operation from C# ASP.NET 4.0

asp.netcodatarestservice-operations

I am connecting to an ODATA Service via a C# ASP.NET application, which has service operations such as:

GetItems(int? itemID, double? price)

I can consume this without issues in my browser, e.g.

http://api.mycompany.com/companycatalogue/GetItems?itemID=4

I understand how to use LINQ to Entities to consume an ODATA service, but can't find a decent explanation of how to consume service operations like the one above in C#. I have made a web reference to the service in my Visual Studio solution.

So far, I have something like this for my usual consuming of the data:

using CompanyCatalogue; //my web reference
...
protected void Page_Load(object sender, EventArgs e)
{
    CompanyCatalogueEntities dataContext = new CompanyCatalogueEntities (new Uri("http://api.mycompany.com/companycatalogue/"));
    var result = from i in dataContext.Items select i;  //just an example

    //this is where I get into problems
    var operationResults = CompanyCatalogue.GetItems(6, 20.5); //I just made this up
}

Any pointers?

Best Answer

OK, got the answer:

using CompanyCatalogue; //my web reference
...
protected void Page_Load(object sender, EventArgs e)
{
    CompanyCatalogueEntities dataContext = new CompanyCatalogueEntities(); 

    DataServiceQuery<GetItemsResult> q = dataContext.CreateQuery<GetItemsResult>("GetItems")
        .AddQueryOption("paramName", 6)
        .AddQueryOption("paramName2", 20.5);

    List<GetItemsResult> items = q.Execute().ToList();
}
Related Topic