Chapter Querying Data
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的更多相关文章
- 《Pro SQL Server Internals, 2nd edition》的CHAPTER 1 Data Storage Internals中的Data Pages and Data Rows(翻译)
数据页和数据行 数据库中的空间被划分为逻辑8KB的页面.这些页面是以0开始的连续编号,并且可以通过指定文件ID和页号来引用它们.页面编号都是连续的,这样当SQL Server增长数据库文件时,从文件中 ...
- AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) Chapter 3. Data Structures Fundamental Data Structures
10410 这题说的是给了一棵多叉树的 bfs遍历轨迹 和dfs 遍历 轨迹 在父节点向叶子节点扩展的时候优先遍历编号较小的节点.我还原出这课树特殊判定 根据bfs的顺序来建立这课树,用一个队列安排要 ...
- Chapter Schema
Chapter Schema Schema是XF的核心,每一个读写方法都有一个相关联的Schema.方法首先解析Schema,然后再根据Schema的配置的执行. 那Schema是什么呢?Schema ...
- XF 文档 - Element Framework Doc
配置篇 Chapter Configuration Schema篇 Chapter Schema 查询篇 Chapter Querying Data 数据更改及验证篇 Chapter Data Mod ...
- 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 ...
- 《计算机科学基础》学习笔记_Part 1 Computer and Data
Technorati Tags: 计算机科学基础,读书笔记 Chapter 1. Introduction Ø 计算机:黑盒,Output Data=f(Input Data, Program) Ø ...
- MySQL Crash Course #06# Chapter 13. 14 GROUP BY. 子查询
索引 理解 GROUP BY 过滤数据 vs. 过滤分组 GROUP BY 与 ORDER BY 之不成文的规定 子查询 vs. 联表查询 相关子查询和不相关子查询. 增量构造复杂查询 Always ...
- (转)MapReduce Design Patterns(chapter 4 (part 1))(七)
Chapter 4. Data Organization Patterns 与前面章节的过滤器相比,本章是关于数据重组.个别记录的价值通常靠分区,分片,排序成倍增加.特别是在分布式系统中,因为这能提高 ...
- [转]Open Data Protocol (OData) Basic Tutorial
本文转自:http://www.odata.org/getting-started/basic-tutorial/ Basic Tutorial The Open Data Protocol (ODa ...
随机推荐
- 转载:使用sklearn进行数据挖掘
目录 1 使用sklearn进行数据挖掘 1.1 数据挖掘的步骤 1.2 数据初貌 1.3 关键技术2 并行处理 2.1 整体并行处理 2.2 部分并行处理3 流水线处理4 自动化调参5 持久化6 回 ...
- 自媒体时代网络脱口秀节目的“五W”分析——以《罗辑思维》为例
摘 要:随着互联网的发展,网络媒介生态的变化正在悄然进行.一大批网络自媒体节目<罗辑思维><晓说><凯子曰>等进入大众视线,成为大众关注的新焦点,其中<罗辑思 ...
- Readonly和Disabled的区别
readonly 把输入的字段设为只读,但是没有禁用 readonly=” readonly”; disabled 禁用一个input元素. disabled="disabled" ...
- RMAN备份与恢复之表空间
通过以下事例来说明表空间的恢复,删除表空间内的数据文件,删除后在针对位于该表空间的表进行插入记录以及实施检查点进程 SQL> select a.tablespace_name from dba_ ...
- Java 开发环境搭建
找到一篇很不錯的Java開發環境搭建的博客, 原文地址為:http://www.cnblogs.com/bribe/p/3377008.html Java 开发环境搭建 一.开发工具获取 1.开发工具 ...
- 用HTML5构建一个流程图绘制工具
在我们的开发工程中经常会使用到各种图,所谓的图就是由节点和节点之间的连接所形成的系统,数学上专门有一个分支叫图论(Graph Theroy).利用图我们可以做很多工具,比如思维导图,流程图,状态机,组 ...
- spring配置中,properties文件以及xml文件配置问题
spring方便我们的项目快速搭建,功能强大,自然也会是体系复杂! 这里说下配置文件properties管理的问题. 一些不涉及到代码逻辑,仅仅只是配置数据,可以放在xxxx.properties文件 ...
- IOS开发-cell的动态高度
tableView中自定义cell的高度随子控件的内容动态变化,也是用的非常多的地方.现在就来处理一个自定义一个里面有文字(多少不定),图片(有无不定)的cell 首先要准备两个模型,一个是存放数据的 ...
- thinkphp在为图片添加png水印不足的处理
thinkphp在为图片加水印的时候.如果水印图片是png图片,透明度处理很不理想,与是做以下处理 在Image.class.php中新增 static function imagecopymerge ...
- 在eclipse中设计BPMN 2.0工作流定义的根本步骤
原文地址:http://www.myexception.cn/eclipse/1863140.html 在eclipse中设计BPMN 2.0工作流定义的基本步骤 1. Activiti问我们提供了A ...