Question

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?

Was it helpful?

Solution

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();
}

OTHER TIPS

This may be help for you. This sample code used to consume the service operation in the WFC Data Service. This service operation(GetRowCount) returns the integer(int) type value. input para name is "code"

    var q = context.CreateQuery<int>("GetRowCount").AddQueryOption("code", "'" + serviceProvider.Code + "'");
            int RecordCount = context.Execute<int>(new Uri(q.RequestUri.ToString().Replace("GetRowCount()", "GetRowCount"))).FirstOrDefault();

Have you tried using HttpWebRequest?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top