OData语法

OData 1-4 OData语法(上)

如果眼下提供OData的服务地址是

http://localhost:9527/ODataService.svc

提供的服务内容例如以下所看到的 (提供了一个WagerInformations)



<?

xml version="1.0" encoding="utf-8" standalone="yes" ?

>

<service xml:base="http://localhost:9527/ODataService.svc/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2007/app">

<workspace>

<atom:title>Default</atom:title>

<collection href="WagerInformations">

<atom:title>WagerInformations</atom:title>

</collection>

</workspace>

</service>



1.基础查询

  1)列出全部的WagerInformations

    http://localhost:9527/ODataService.svc/WagerInformations

  2)依照主键查询

    http://localhost:9527/ODataService.svc/WagerInformations(1)

     PS:在.net里面一般使用DataServiceKeyAttribute标识主键

  3)获取某个对象的一个成员

    http://localhost:9527/ODataService.svc/WagerInformations(1)/EventName

    获取主键为1的WagerInformations的EventName属性

  4)假设这个属性还有属性 那么依此类推

    http://localhost:9527/ODataService.svc/WagerInformations(1)/Event/EventDateTime

    另外不要试图获取原始类型的一些属性 - -# 比如返回 String的Length属性

  5) $value 方案3返回对象的一个成员用的是Xml的数据格式.实际上我们非常多时候不须要那么多的标签,仅仅想拿返回值

    那么使用url http://localhost:9527/ODataService.svc/WagerInformations(1)/EventName/$value

    方案3的数据 <?xml version="1.0" encoding="utf-8" standalone="yes" ?> <EventName xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices">test 1</EventName>

    方案5的数据 test 1

  6) $filter  条件表达式

    查询EventName 等于 "test 1" 的表达式例如以下

    http://localhost:9527/ODataService.svc/WagerInformations?$filter=EventName  eq 'test 1'

    查询时间:

    http://localhost:9527/ODataService.svc/WagerInformations?$filter=EventDateTime eq DateTime'2010-12-21T10:10:19.390625'

     组合查询表达式: and操作

    http://localhost:9527/ODataService.svc/WagerInformations?$filter=(EventDateTime eq DateTime'2010-12-21T10:10:19.390625' ) and (BusinessUnitCode eq '2')

  

 

下面是运算符列表 

Operator

Description

C#         equivalent

eq

equals

==

ne

not equal

!=

gt

greater than

>

ge

greater than or equal

>=

lt

less than

<

le

less than or equal

<=

and

and

&&

or

or

||

()

grouping

()

 

 




OData 1-5 OData语法(下)


7) $expand 包括属性和关系

  如果的WagerInformation拥有一个属性 UserInformation User 表示用户信息,  还有一个属性 IEnumerable<CommonInformation> Commons 表示评论信息

  使用 http://localhost:9527/ODataService.svc/WagerInformations?$expand=User ,Commons

  返回的信息中就会包括相关类 (用于主外键关系)

  - -# 假设不手动指定 而是自己主动关联....那就悲剧了 可能数据库中的全部表都有联系...然后把整个数据库返回.....

  曾经做过非常囧的事情.就是开了级联删除...然后删除了一个非常主要的配置项.....整个数据库基本空了

8) $select 查询字段的列表(和sql中select后面的表达式一样)

  下面url仅仅想返回查询全部信息的EventName属性

  http://localhost:9527/ODataService.svc/WagerInformations?

$select=EventName  

  假设WagerInformation有一个User属性 其包括一个UserName那么查询username的url例如以下

  http://localhost:9527/ODataService.svc/WagerInformations?$select=User/UserName

9) $count 查询数量

  http://localhost:9527/ODataService.svc/WagerInformations/$count

  返回的是真实数据不包括不论什么修饰 (raw data) 传回的http body中就仅仅有一个 "5"  (不包括引號)

10) $orderby  排序

  下面表达式依照BusinessUnitCode 降序 ,然后 EventName 升序排列

   http://localhost:9527/ODataService.svc/WagerInformations?

$orderby=BusinessUnitCode desc,EventName asc

 

11) $top

  在10的基础上 假设我仅仅想返回第一条数据 那么例如以下

  http://localhost:9527/ODataService.svc/WagerInformations?

$orderby=BusinessUnitCode desc,EventName asc&$top=1

  这里依旧还是用& 来分隔不同的表达式

 

12) $skip

  这东西一般和$top配合来分页

  下面表达式跳过第一条, 然后返回最多10条数据

  http://localhost:9527/ODataService.svc/WagerInformations?$top=10&$skip=1

13) $inlinecount

  在分页取数据的时候,常常要同一时候统计总记录数

  下面表达式在返回分页数据的同一时候,顺便同一时候返回全部的记录数

  http://localhost:9527/ODataService.svc/WagerInformations?

$top=2&$skip=2&$inlinecount=allpages

  假设表达式中有$filter  条件表达式 ,那么返回的就是符合条件的全部数据的数量

  http://localhost:9527/ODataService.svc/WagerInformations?$filter=BusinessUnitCode eq '1'&$inlinecount=allpages

14) $skiptoken

  比如游标或者书签的一个东西

15)$links

  获取相关实体的url 

  http://localhost:9527/ODataService.svc/WagerInformations(1)/$links/User

16)$metadata

  显示元数据

  http://localhost:9527/ODataService.svc/$metadata 



200(OK)

202(Accepted)

204(No Content)

400(Bad Request)

404(Not Found)

405(Method Not Supported)

412(Precondition Failed)

500(Internal Server Error)

版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值