OData语法

 

 

 

 

 

1.基础查询sql

  1)列出全部的WagerInformations数据库

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

  2)按照主键查询url

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

     PS:在.net里面通常使用DataServiceKeyAttribute标识主键.net

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

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

    获取主键为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

()

 

 

 

 

 

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 

相关文章
相关标签/搜索