不积跬步,无以至千里。 不积小流,无以成江海。

最近在使用Odata,从官网上摘录一些常用的Odta v2指令记录一下。

运算符

Operator Description Example
Logical Operators 逻辑操作
Eq Equal - 等于 /Suppliers?$filter=Address/City eq ‘Redmond’
Ne Not equal - 不等于 /Suppliers?$filter=Address/City ne ‘London’
Gt Greater than - 大于 /Products?$filter=Price gt 20
Ge Greater than or equal -大于或等于 /Products?$filter=Price ge 10
Lt Less than - 小于 /Products?$filter=Price lt 20
Le Less than or equal - 小于或等于 /Products?$filter=Price le 100
And Logical and - 逻辑与 /Products?$filter=Price le 200 and Price gt 3.5
Or Logical or - 逻辑或 /Products?$filter=Price le 3.5 or Price gt 200
Not Logical negation - 逻辑非 /Products?$filter=not endswith(Description,’milk’)
Arithmetic Operators 算术操作
Add Addition - 加 /Products?$filter=Price add 5 gt 10
Sub Subtraction -减 /Products?$filter=Price sub 5 gt 10
Mul Multiplication - 乘 /Products?$filter=Price mul 2 gt 2000
Div Division - 除 /Products?$filter=Price div 2 gt 4
Mod Modulo - 模 /Products?$filter=Price mod 2 eq 0
Grouping Operators
( ) Precedence grouping /Products?$filter=(Price sub 5) gt 10

函数

Function Example
String Functions
bool substringof(string po, string p1) https://services.odata.org/Northwind/Northwind.svc/Customers?$filter=substringof(‘Alfreds’, CompanyName) eq true
bool endswith(string p0, string p1) https://services.odata.org/Northwind/Northwind.svc/Customers?$filter=endswith(CompanyName, ‘Futterkiste’) eq true
bool startswith(string p0, string p1) https://services.odata.org/Northwind/Northwind.svc/Customers?$filter=startswith(CompanyName, ‘Alfr’) eq true
int length(string p0) https://services.odata.org/Northwind/Northwind.svc/Customers?$filter=length(CompanyName) eq 19
int indexof(string p0, string p1) https://services.odata.org/Northwind/Northwind.svc/Customers?$filter=indexof(CompanyName, ‘lfreds’) eq 1
string replace(string p0, string find, string replace) https://services.odata.org/Northwind/Northwind.svc/Customers?$filter=replace(CompanyName, ‘ ‘, ‘’) eq ‘AlfredsFutterkiste’
string substring(string p0, int pos) https://services.odata.org/Northwind/Northwind.svc/Customers?$filter=substring(CompanyName, 1) eq ‘lfreds Futterkiste’
string substring(string p0, int pos, int length) https://services.odata.org/Northwind/Northwind.svc/Customers?$filter=substring(CompanyName, 1, 2) eq ‘lf’
string tolower(string p0) https://services.odata.org/Northwind/Northwind.svc/Customers?$filter=tolower(CompanyName) eq ‘alfreds futterkiste’
string toupper(string p0) https://services.odata.org/Northwind/Northwind.svc/Customers?$filter=toupper(CompanyName) eq ‘ALFREDS FUTTERKISTE’
string trim(string p0) https://services.odata.org/Northwind/Northwind.svc/Customers?$filter=trim(CompanyName) eq ‘Alfreds Futterkiste’
string concat(string p0, string p1) https://services.odata.org/Northwind/Northwind.svc/Customers?$filter=concat(concat(City, ‘, ‘), Country) eq ‘Berlin, Germany’
Date Functions
int day(DateTime p0) https://services.odata.org/Northwind/Northwind.svc/Employees?$filter=day(BirthDate) eq 8
int hour(DateTime p0) https://services.odata.org/Northwind/Northwind.svc/Employees?$filter=hour(BirthDate) eq 0
int minute(DateTime p0) https://services.odata.org/Northwind/Northwind.svc/Employees?$filter=minute(BirthDate) eq 0
int month(DateTime p0) https://services.odata.org/Northwind/Northwind.svc/Employees?$filter=month(BirthDate) eq 12
int second(DateTime p0) https://services.odata.org/Northwind/Northwind.svc/Employees?$filter=second(BirthDate) eq 0
int year(DateTime p0) https://services.odata.org/Northwind/Northwind.svc/Employees?$filter=year(BirthDate) eq 1948
Math Functions
double round(double p0) https://services.odata.org/Northwind/Northwind.svc/Orders?$filter=round(Freight) eq 32d eq 32)
decimal round(decimal p0) https://services.odata.org/Northwind/Northwind.svc/Orders?$filter=round(Freight) eq 32
double floor(double p0) https://services.odata.org/Northwind/Northwind.svc/Orders?$filter=round(Freight) eq 32d eq 32)
decimal floor(decimal p0) https://services.odata.org/Northwind/Northwind.svc/Orders?$filter=floor(Freight) eq 32
double ceiling(double p0) https://services.odata.org/Northwind/Northwind.svc/Orders?$filter=ceiling(Freight) eq 33d eq 33)
decimal ceiling(decimal p0) https://services.odata.org/Northwind/Northwind.svc/Orders?$filter=floor(Freight) eq 33
Type Functions
bool IsOf(type p0) https://services.odata.org/Northwind/Northwind.svc/Orders?$filter=isof('NorthwindModel.Order')
bool IsOf(expression p0, type p1) https://services.odata.org/Northwind/Northwind.svc/Orders?$filter=isof(ShipCountry, ‘Edm.String’)

格式

$format Value Response Media Type
Atom application/atom+xml
Xml application/xml
Json application/json
Any other IANA-defined content type Any IANA-defined content type
A service-specific value indicating a format specific to the specific OData service Any IANA-defined content type

Examples

https://services.odata.org/OData/OData.svc/Products?$format=atom

odata分页

Examples

1
https://services.odata.org/OData/OData.svc/Products?$top=pageSize&skip=(page-1)*pageSize&$inlinecount=allpages
  • $top

    表示取多少条数据

  • $skip

    表示跳过多少条数据

  • $inlinecount=allpages

    返回总记录数 __count