Skip to main content
 首页 » 编程设计

c#之ODATA 从 C# ASP.NET 4.0 消费服务操作

2024年02月24日19sky-heaven

我正在通过 C# ASP.NET 应用程序连接到 ODATA 服务,该应用程序具有如下服务操作:

GetItems(int? itemID, double? price) 

我可以在我的浏览器中毫无问题地使用它,例如

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

我了解如何使用 LINQ to Entities 来使用 ODATA 服务,但找不到关于如何在 C# 中使用服务操作的合适解释。我已在我的 Visual Studio 解决方案中对该服务进行了 Web 引用。

到目前为止,对于我通常使用的数据,我有这样的东西:

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 
} 

有什么建议吗?

请您参考如下方法:

好的,得到答案:

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