如何创建OData的查询表达式通过id来获取 [英] How to create query expression for Odata for get by Id
问题描述
我已经创建OData服务,现在我想在消费客户端此服务。我想创建一个表达式,比如在C#查询表达式以下网址 -
I have created an OData service and now I am trying to consume this service at client side. I wants to create an expression such as for the below url in the c# query expression-
的 http://odata.org/Product-Service/Product(150)
上面的网址是工作在浏览器不错,但我想在C#中上面的URL创建查询表达式。任何帮助将大大明显。
The above url is working fine in browsers but I want to create query expression in the C# for the above url. Any help would be greatly appreciable.
推荐答案
您可以使用 DataServiceContext
+ 化DataServiceQuery
在 System.Data.Services.Client
来打的URL。记住没有查询被执行,直到调用第一个()因延迟加载。
You could use a DataServiceContext
+ DataServiceQuery
in System.Data.Services.Client
to hit the Url. Remember no query is executed until the call to First() due to lazy loading.
var context = new DataServiceContext(new Uri("http://odata.org/Product-Service"), DataServiceProtocolVersion.V3);
var query = context.CreateQuery<Product>("Product");
Product product = query.Where(p => p.Id == 150).First();
以上应该下定决心的 http://odata.org/Product-Service/Product(150),你可以通过查看 query.Entities检查
集合。该集合将包含一个URI中的每个实体
The above should resolve to http://odata.org/Product-Service/Product(150) which you can check by looking at the query.Entities
collection. Each entity in the collection will contain a Uri.
此外,如果你的产品类包含一个导航属性,你需要这样添加扩展查询选项:
Also if your Product class contains a navigation property, you will need to add the expand query option thus:
var query = context.CreateQuery<Product>("Product").
AddQueryOption("$expand", "NavigationProperty");
这篇关于如何创建OData的查询表达式通过id来获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!