Entity Framework 学习初级篇4--Entity SQL

 

Entity SQL ADO.NET 实体框架 提供的 SQL 类语言,用于支持 实体数据模型 (EDM)Entity SQL 可用于对象查询和使用 EntityClient 提供程序执行的查询。

l           关键字

Value关键字

 ESQL 提供了 SELECT VALUE 子句以跳过隐式行构造。SELECT VALUE 子句中只能指定一项。在使用这样的子句时,将不会对 SELECT 子句中的项构造行包装器,并且可生成所要形状的集合,例如:SELECT VALUE it FROM NorthwindEntities.Customers as it

it关键字

it 出现在 ESQL , 查询对象的别名默认值 "it" 改成其他字符串,例如:

"SELECT VALUE it FROM NorthwindEntities.Customers as it "

l           注释:

Entity SQL 查询可以包含注释。注释行以两个短划线 (--) 开头。

"SELECT VALUE it FROM NorthwindEntities.Customers as it  -- this a comment "

l           Select查询

例如:

SELECT VALUE it FROM NorthwindEntities.Customers as it

l           参数

参数是在esql之外定义的变量,每个参数都有名称和类型,参数名称在查询表达式中定义,并以@符号作为前缀。例如:

Select VALUE c from NorthwindEntities.Customers as c where c.CustomerID=@customerID

l           聚合

Enity SQL不支持 * ,所以esql不支持count(*),而是使用count(0),例如:

Select count(0) from NorthwindEntities.Customers

l           分页SKIP/LIMIT

可以通过在 ORDER BY 子句中使用 SKIP LIMIT 子子句执行物理分页。若要以确定的方式执行物理分页,应使用 SKIP LIMIT。如果您只是希望以非确定的方式限制结果中的行数,则应使用 TOPTOP SKIP/LIMIT 是互斥的

使用SKIP/LIMIT分页,esql代码如下:

Select value c from NorthwindEntities.Customers as c order by c.CustomerID skip 0 limit 10

l           TOP

SELECT 子句可以在可选的 ALL/DISTINCT 修饰符之后具有可选的 TOP 子子句。TOP 子子句指定查询结果中将只返回第一组行。esql代码如下:

Select top(10) c.CustomerID from NorthwindEntities.Customers as c order by c.CustomerID

l           NULL处理

Null 文本与 Entity SQL 类型系统中的任何类型都兼容,可以使用cast进行类型转换,例如:

select cast(c.region as string) from NorthwindEntities.Customers as c order by c.CustomerID limit 10

其中, Nvarchar等可以成string,数字类型可以转成int32,其他的类型转换类似。如果无法完成转换,则将报异常。还有可以处理的方法有treat

l           标识符

Entity SQL 提供两种标识符:简单标识符和带引号的标识符

简单标识符:Entity SQL 中的简单标识符是字母数字和下划线字符的序列。标识符的第一个字符必须是字母字符(a-z A-Z)。

带引号的标识符:带引号的标识符是括在方括号 ([]) 中的任何字符序列。带中文的部分,请使用方括号包括起来,否则会报如下异常信息:简单标识符“中文”只能包含基本拉丁字符。若要使用UNICODE 字符,请使用转义标识符

正确的代码如下:

Select c.CustomerID as [中文字符] from NorthwindEntities.Customers as c order by c.CustomerID skip 0 limit 10

l           ROW

Esql可使用row来构建匿名的结构类型的纪录。例如:

SELECT VALUE row(p.ProductID as ProductID,p.ProductName as ProductName) FROM NorthwindEntities.Products as p order by p.ProductID LIMIT 10

l           Key

提取引用或实体表达式的键。如下esql语句,直接返回Customer表的主键:

string esql = "SELECT value key(c) FROM NorthwindEntities.Customers as c order by c.CustomerID LIMIT 10"

l           CreateRef/ref/deref

CreateRef创建对实体集中的实体的引用。

ref返回对实体实例的引用,之后就可以当作实体来访问其属性,esql语句如下:

SELECT ref(c).CustomerID FROM NorthwindEntities.Customers as c order by c.CustomerID LIMIT 10

deref运算符取消引用一个引用值,并生成该取消引用的结果。

l           CASE语句:

 string esql = "using SqlServer;select case when len(trim(c.CustomerID))==0 then true else false end  from NorthwindEntities.Customers as c order by c.CustomerID limit 10";

l           运算符

Esql支持的运算符有:加+、减-、乘*、除/、取模%-负号。Esql语句如下:

select 100/2 as OP from NorthwindEntities.Customers as c order by c.CustomerID limit 10

l           比较运算符

Esql支持的比较运算符有:=,>,>=,IS [NOT] NULL,<,[NOT] BETWEEN,!=,<>,[NOT] LIKEEsql语句如下:

select value p from NorthwindEntities.Products as p where p.UnitPrice > 20 order by p.ProductID limit 10

l           逻辑运算符

Esql支持的逻辑运算符有:and(&&),not(!),or(||)Esql语句如下:

select value p from NorthwindEntities.Products as p where p.UnitPrice > 20 and p.UnitPrice<100 order by p.ProductID limit 10

select value p from NorthwindEntities.Products as p where p.UnitPrice > 20 && p.UnitPrice<100 order by p.ProductID limit 10

l           字符串连接运算符。

加号 (+) Entity SQL 中可将字符串串联起来的唯一运算符。Esql语句如下:

select c.CustomerID + c.ContactName from NorthwindEntities.Customers as c  order by c.CustomerID limit 10

l           嵌套查询

Entity SQL 中,嵌套查询必须括在括号中,将不保留嵌套查询的顺序

select c1.CustomerID from( select value c from NorthwindEntities.Customers as c  order by c.CustomerID limit 10) as c1

l           日期时间函数

Esql提供的日期时间函数有:CurrentDateTime()获取当前服务器的日期时间,还有month,dayyearsecond, Minute Hour等。例如:

select CurrentDateTime()  from NorthwindEntities.Customers as c  order by c.CustomerID limit 10

l           字符串函数

Esql提供的字符串函数有:Concat,IndexOf,Left,Length,Ltrim,Replace,Reverse,Rtrim,SubString,Trim,ToLower,ToUpper.例如:

select Reverse(p.ProductName) as ProductName from NorthwindEntities.Products as p  order by p.ProductID limit 10

l           GUID

Esql提供newguid()函数,产生一个新的Guid。例如:

select newguid()  from NorthwindEntities.Customers as c  order by c.CustomerID limit 10

l           数学函数:

Abs,Ceiling,Floor,Round

l           统计函数:

AvgBigCount,Count,Max,Min,StDev,Sum

l           位计算函数

如果提供 Null输入,则这些函数返回 Null。这些函数的返回类型与参数类型相同。如果函数采用多个参数,则这些参数必须具有相同的类型。若要对不同类型执行位运算,则需要显式强制转换为相同类型.

BitWiseAnd,BitWiseNot,BitWiseOr,BitWiseXor

l           命名空间

Entity SQL 引入命名空间以避免全局标识符(如类型名称、实体集、函数等)出现名称冲突。Entity SQL 中的命名空间支持与 .NET Framework 中的命名空间支持类似。

Entity SQL 提供两种形式的 USING 子句:限定命名空间(其中,提供较短的别名以表示命名空间)和非限定命名空间,如下例所示:

USING System.Data;

USING tsql = System.Data;

例如:

string esql = "using System; select cast(p.UnitPrice as Int32)  from NorthwindEntities.Products as p  order by p.ProductID limit 10 ";

string esql = "using System;using SqlServer; select (cast(p.UnitPrice as Int32)),SqlServer.ltrim(p.ProductName) as nameLen from NorthwindEntities.Products as p  order by p.ProductID limit 10 ";

 

最后,简单说一下EsqlT-Sql的某些差异:

l           Entity SQL 中的所有列引用都必须用表别名限定.

l           Esql不支持Anyall限定运算符以及*运算

l           Entity SQL 当前未提供对 DML 语句(insertupdatedelete)的支持。

l           Entity SQL 的当前版本未提供对 DDL 的支持。

posted @ 2009-05-11 00:35  ♂风车车  阅读(26711)  评论(22编辑  收藏  举报