Chapter Querying Data

XF获取数据的三种方法:

其中参数schema,参见 Chapter Schema。

下面列出一个后面将会用到的schema片段,称之为片段A:

<User Set="Users">

...

<Employee.Name Element="Employee" Field="Name" />

...

</User>

方法一:ElementQuerier.GetSet("User", "Id,UserName,IsLockedOut,Employee.Name", "EmployeeId eq 1", "Id desc", schema);

内部会调用3.调用时,已在schema插入片段A。

方法二:.Linq,需要using XData.Data.Query。返回 XElement。

ElementContext.GetElementSet("User", schema)

.Where(x => x.Element("EmployeeId").Value == "1")

.OrderByDescending(x => int.Parse(x.Element("Id").Value))

.Select(x => new XElement(x.Name, x.Element("Id"), x.Element("UserName"), x.Element("IsLockedOut"), x.Element("Employee.Name")));

内部会调用3.调用时,schema 中应有片段A。

方法三:.ElementContext.GetSet(query, schema);返回 XElement。

schema 中应有片段A。

参数query:

<User>

<Select>

<Id />

<UserName />

<IsLockedOut />

<Employee.Name />

</Select>

<OrderByDescending>

<Id />

</OrderByDescending>

<!-- Linq -->

<Where>

<BinaryExpression NodeType="Equal">

<MemberExpression NodeType="MemberAccess" Member="Id" />

<ConstantExpression NodeType="Constant" DataType="System.Int32" Value="1" />

</BinaryExpression>

</Where>

<!-- ElementQuerier -->

<Filter>

<Value>EmployeeId eq 1</Value>

</Filter>

</User>

直接的Sql

Database.SqlQuery("User",

"SELECT u.Id,u.UserName,u.IsLockedOut,e.Name [Employee.Name] FROM Users u LEFT JOIN Employees e ON u.EmployeeId = e.Id WHERE u.EmployeeId = 1 ORDER BY u.Id DESC;");返回 IEnumerable<XElement>

比较发现,方法二Linq不够简洁,方法三过于底层。因此更为推荐方法一。以下叙述,均以方法一为主。

ElementQuerier设计极力模仿OData,称之为XData(将在XData介绍)。下面列出XData支持的操作符和函数:

Operators:

OData

SQL Server

OData

SQL Server

eq

=

IsLockedOut eq true

IsLockedOut = 1

eq null

IS NULL

Descr eq null

Descr IS NULL

ne

!=

Name ne 'Milk'

Name != 'Milk'

ne null

IS NOT NULL

Descr ne null

Descr IS NOT NULL

gt

>

Id gt 1

Id > 1

ge

>=

CreatedDate ge datetime’2012-12-1’

CreatedDate >= ’2012-12-1’

lt

<

Allowance lt 1000

Allowance < 1000

le

<=

Allowance le 1000.1

Allowance <= 1000.1

and

AND

(IsLockedOut eq true) and (Id eq 1)

(IsLockedOut = 1) and (Id = 1)

or

OR

(Id gt 1) OR (Allowance le 1234.56)

(Id > 1) OR (Allowance <= 1234.56)

not

NOT

not (Id gt 1)

NOT (Id > 1)

has

Not supported

has

Not supported

add

+

Allowance add 12.34

Allowance + 12.34

sub

-

Allowance sub 12.34

Allowance - 12.34

mul

*

Allowance mul 2.15

Allowance * 2.15

div

/

Allowance div 2.0

Allowance / 2.0

mod

%

Rating mod 5

Rating % 5

Canonical Functions:

OData

SQL Server

OData

SQL Server

contains(XData)

Operator IN

contains((1,3,5),Id)

Id IN (1,3,5)

contains

Operator LIKE

contains(CompanyName,'Alfreds')

CompanyName LIKE '%Alfreds%'

endswith

Operator LIKE

endswith(CompanyName,'Futterkiste')

CompanyName LIKE '%Futterkiste'

startswith

Operator LIKE

startswith(CompanyName,'Alfr')

CompanyName LIKE 'Alfr%'

length

DATALENGTH

length(CompanyName)

DATALENGTH(CompanyName)

indexof

CHARINDEX

indexof(CompanyName,'lfreds')

CHARINDEX('lfreds', CompanyName) -1

substring

SUBSTRING

substring(CompanyName, 1)

SUBSTRING(CompanyName, 2, 2147483647)

substring

SUBSTRING

substring(CompanyName, 1, 2)

SUBSTRING(CompanyName, 2, 2)

tolower

LOWER

tolower(CompanyName)

LOWER(CompanyName)

toupper

UPPER

toupper(CompanyName)

UPPER(CompanyName)

trim

LTRIM and RTRIM

trim(CompanyName)

LTRIM(RTRIM(CompanyName))

concat(XData)

Operator +

concat(City, ', ', Country)

City + ', ' + Country

year

DATEPART

year(BirthDate)

DATEPART(YEAR, BirthDate)

month

DATEPART

month(BirthDate)

DATEPART(MONTH, BirthDate)

day

DATEPART

day(BirthDate)

DATEPART(DAY, BirthDate)

hour

DATEPART

hour(BirthDate)

DATEPART(HOUR, BirthDate)

minute

DATEPART

minute(BirthDate)

DATEPART(MINUTE, BirthDate)

second

DATEPART

second(BirthDate)

DATEPART(SECOND, BirthDate)

fractionalseconds

DATEPART

fractionalseconds(BirthDate)

DATEPART(MILLISECOND, BirthDate) /1000.0

date

CAST and CONVERT

date(BirthDate)

CAST(CONVERT(char(10), BirthDate,120) AS datetime)

time

Not supported

time(BirthDate)

Not supported

totaloffsetminutes

Not supported

totaloffsetminutes(datetime)

Not supported

now

GETDATE

now()

GETDATE()

Utcnow(XData)

GETUTCDATE

utcnow()

GETUTCDATE()

maxdatetime

Not supported

maxdatetime()

Not supported

mindatetime

Not supported

mindatetime()

Not supported

totalseconds

Not supported

totalseconds

Not supported

round

ROUND

round(Amount)

ROUND(Amount, 0)

floor

FLOOR

floor(Amount)

FLOOR(Amount)

ceiling

CEILING

ceiling(Amount)

CEILING(Amount)

isof

Not supported

isof

Not supported

cast

Not supported

cast

Not supported

注:现在明白古怪的"EmployeeId eq 1"是什么了吧。

高阶ElementQuerier(XData)查询

调用如下方法,会返回一个“聚合”结构的XElement:

ElementQuerier.GetSet("Job","Id,Name","Allowance gt 0",null, expands,schema);

参数schema,如前。

参数expands,类型为IEnumerable<Expand>,其内容为:

仔细查看返回的XElement,重点查看层次结构:

<Jobs>

<Job>

<Id>1</Id>

<Name>CEO</Name>

<Employees>

<Employee>

<Id>1</Id>

<Name>Carl Zeiss</Name>

<JobId>1</JobId>

<Users>

<User>

<Id>1</Id>

<UserName>Carl</UserName>

</User>

<User>

<Id>5</Id>

<UserName>Admin</UserName>

</User>

</Users>

</Employee>

</Employees>

</Job>

<Job>

<Id>2</Id>

<Name>CFO</Name>

<Employees>

<Employee>

<Id>2</Id>

<Name>Cherry Wong</Name>

<JobId>2</JobId>

<Users>

<User>

<Id>2</Id>

<UserName>Cherry</UserName>

</User>

</Users>

</Employee>

</Employees>

</Job>

</Jobs>

如前所述,ElementQuerier.GetSet内部会调用ElementContext.GetSet,这里也是一样,在调用前会将下列片段B,插入schema参数中。

schema 片段B:

<Job Set="Jobs">

...

<Employees Element="Employee">

<Users Element="User" Select="Id,UserName" />

</Employees>

...

</Jobs>

注:上述方法,其实由下面这个链接引发。

http://localhost:2012/XData/Jobs?$expand=Employees($expand=Users($select=Id,UserName))&$select=Id,Name&$filter=Allowance%20gt%200

上面为master -details,再来看一个detail-master 查询,注意$filter里面的Job与User没有直接关系,而是通过ReferencePath连接:

http://localhost:2012/XData/Users?$expand=Employee&$select=Id,EmployeeId,UserName&$filter=Job.Allowance%20gt%200

返回结果:

<Users>

<User>

<Id>1</Id>

<EmployeeId>1</EmployeeId>

<UserName>Carl</UserName>

<Employee>

<Id>1</Id>

<Name>Carl Zeiss</Name>

<JobId>1</JobId>

</Employee>

</User>

<User>

<Id>2</Id>

<EmployeeId>2</EmployeeId>

<UserName>Cherry</UserName>

<Employee>

<Id>2</Id>

<Name>Cherry Wong</Name>

<JobId>2</JobId>

</Employee>

</User>

<User>

<Id>5</Id>

<EmployeeId>1</EmployeeId>

<UserName>Admin</UserName>

<Employee>

<Id>1</Id>

<Name>Carl Zeiss</Name>

<JobId>1</JobId>

</Employee>

</User>

</Users>

多对多关系的查询:

http://localhost:2012/XData/Users?$select=Id,UserName&$filter=Id%20lt%203&$expand=Roles($select=Id,RoleName)

返回结果:

<Users>

<User>

<Id>1</Id>

<UserName>Carl</UserName>

<Roles>

<Role>

<Id>1</Id>

<RoleName>Admin</RoleName>

</Role>

<Role>

<Id>2</Id>

<RoleName>User</RoleName>

</Role>

</Roles>

</User>

<User>

<Id>2</Id>

<UserName>Cherry</UserName>

<Roles>

<Role>

<Id>2</Id>

<RoleName>User</RoleName>

</Role>

</Roles>

</User>

</Users>

XF遇到多对多关系“连接”就此“断路”,所以以下查询是错误的:

http://localhost:2012/XData/Users?$select=Id,UserName&$filter=Id%20lt%203&$expand=Roles($select=Id,RoleName$expand=Routes)

ElementQuerier 除了上面介绍的GetSet,还有下列主要方法:

public XElement Find(string elementName, string[] key, string select, XElement schema)

public XElement Find(string elementName, string[] key, string select, IEnumerable<Expand> expands, XElement schema)

public XElement GetPage(string elementName, string select, string filter, string orderBy, int skip, int take, XElement schema)

public XElement GetPage(string elementName, string select, string filter, string orderBy, int skip, int take, IEnumerable<Expand> expands, XElement schema)

public int GetCount(string elementName, string filter, XElement schema)

public XElement GetDefault(string elementName, string select, XElement schema)

为什么会有GetDefault方法,见如下查询的结果:

http://localhost:2012/XData/Employees/$default?$select=Id,Name,JobId,Jobs.Name

注意下面的Jobs.Name,它是通过"Where JobId = 3"查询得到的。

<Employees>

<Id/>

<Name/>

<JobId>3</JobId>

<Jobs.Name>Clerk</Jobs.Name>

</Employees>

Chapter Querying Data的更多相关文章

  1. 《Pro SQL Server Internals, 2nd edition》的CHAPTER 1 Data Storage Internals中的Data Pages and Data Rows(翻译)

    数据页和数据行 数据库中的空间被划分为逻辑8KB的页面.这些页面是以0开始的连续编号,并且可以通过指定文件ID和页号来引用它们.页面编号都是连续的,这样当SQL Server增长数据库文件时,从文件中 ...

  2. AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) Chapter 3. Data Structures Fundamental Data Structures

    10410 这题说的是给了一棵多叉树的 bfs遍历轨迹 和dfs 遍历 轨迹 在父节点向叶子节点扩展的时候优先遍历编号较小的节点.我还原出这课树特殊判定 根据bfs的顺序来建立这课树,用一个队列安排要 ...

  3. Chapter Schema

    Chapter Schema Schema是XF的核心,每一个读写方法都有一个相关联的Schema.方法首先解析Schema,然后再根据Schema的配置的执行. 那Schema是什么呢?Schema ...

  4. XF 文档 - Element Framework Doc

    配置篇 Chapter Configuration Schema篇 Chapter Schema 查询篇 Chapter Querying Data 数据更改及验证篇 Chapter Data Mod ...

  5. Coursera, Big Data 3, Integration and Processing (week 1/2/3)

    This is the 3rd course in big data specification courses. Data model reivew 1, data model 的特点: Struc ...

  6. 《计算机科学基础》学习笔记_Part 1 Computer and Data

    Technorati Tags: 计算机科学基础,读书笔记 Chapter 1. Introduction Ø  计算机:黑盒,Output Data=f(Input Data, Program) Ø ...

  7. MySQL Crash Course #06# Chapter 13. 14 GROUP BY. 子查询

    索引 理解 GROUP BY 过滤数据 vs. 过滤分组 GROUP BY 与 ORDER BY 之不成文的规定 子查询 vs. 联表查询 相关子查询和不相关子查询. 增量构造复杂查询 Always ...

  8. (转)MapReduce Design Patterns(chapter 4 (part 1))(七)

    Chapter 4. Data Organization Patterns 与前面章节的过滤器相比,本章是关于数据重组.个别记录的价值通常靠分区,分片,排序成倍增加.特别是在分布式系统中,因为这能提高 ...

  9. [转]Open Data Protocol (OData) Basic Tutorial

    本文转自:http://www.odata.org/getting-started/basic-tutorial/ Basic Tutorial The Open Data Protocol (ODa ...

随机推荐

  1. 【转载】springMVC表单校验+全注解

    在这篇文章中,我们将学习如何使用Spring表单标签, 表单验证使用 JSR303 的验证注解,hibernate-validators,提供了使用MessageSource和访问静态资源(如CSS, ...

  2. 配置samba服务器

    公司需要一台用于共享的文件服务器,考虑使用Linux系统,听说会比windows的文件系统好管理,了解不多,人云亦云了! 这里需要用到samba系统,安装比较简单,安装后需要进行配置才能访问. 修改s ...

  3. smarty函数

    内置函数(重要的几个): <{html_checkboxes name='nation' values=$code output=$name selected=$selid separator= ...

  4. 01TCP/IP基础

    ISO/OSI参考模型: OSI(open system interconnection)开放系统互联模型是由ISO(International Organization for Standardiz ...

  5. Python(正则 Time datatime os sys random json pickle模块)

    正则表达式: import re #导入模块名 p = re.compile(-]代表匹配0至9的任意一个数字, 所以这里的意思是对传进来的字符串进行匹配,如果这个字符串的开头第一个字符是数字,就代表 ...

  6. 记一下一些比较有意思的第三方API

    野狗,第三方后端通信用的:https://www.wilddog.com/ 花瓣网,用来做设计的:http://huaban.com/ Ping++,聚合支付接口:https://www.pingxx ...

  7. 如何在VBA窗体中使用 DataGrid 控件?

    1.下载MSDATGRD.cab,下载路径如下:http://www.findthatzipfile.com/search-10500733-hZIP/winrar-winzip-download-m ...

  8. [CSS]理解line-height

    字面意思 "行高"顾名思意指一行文字的高度.具体来说是指两行文字间基线之间的距离.基线实在英文字母中用到的一个概念,我们刚学英语的时使用的那个英语本子每行有四条线,其中底部第二条线 ...

  9. PLSQL_Oracle Trigger触发器的基本概念和用法

    2014-06-14 Created By BaoXinjian

  10. RMQ问题(线段树+ST算法)

    转载自:http://kmplayer.iteye.com/blog/575725 RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ ...