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。如果您只是希望以非确定的方式限制结果中的行数,则应使用 TOP。TOP 和 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] LIKE。Esql语句如下:

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,day,year,second, 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           统计函数:

Avg,BigCount,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 ";

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

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

l           Esql不支持Any,all限定运算符以及*运算

l           Entity SQL 当前未提供对 DML 语句(insert、update、delete)的支持。

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

Entity Framework 学习初级篇4--Entity SQL的更多相关文章

  1. Entity Framework学习初级篇2

    Entity Framework 学习初级篇2--ObjectContext.ObjectQuery.ObjectStateEntry.ObjectStateManager类的介绍 本节,简单的介绍E ...

  2. Entity Framework 学习初级篇--基本操作:增加、更新、删除、事务(转)

    摘自:http://www.cnblogs.com/xray2005/archive/2009/05/17/1458568.html 本节,直接写通过代码来学习.这些基本操作都比较简单,与这些基本操作 ...

  3. Entity Framework 学习初级篇1--EF基本概况

    转自:http://www.cnblogs.com/Tally/archive/2012/09/14/2685011.html 最近在学习研究微软的EF,通过这时间的学习研究,感觉这个EF目前来说还不 ...

  4. Entity Framework 学习初级篇7--基本操作:增加、更新、删除、事务

    本节,直接写通过代码来学习.这些基本操作都比较简单,与这些基本操作相关的内容在之前的1至6节基本介绍完毕. l           增加: 方法1:使用AddToXXX(xxx)方法:实例代码如下: ...

  5. Entity Framework学习初级篇1--EF基本概况《转》

    最近在学习研究微软的EF,通过这时间的学习研究,感觉这个EF目前来说还不是很完善,半成品.不过,据说在.Net4.0中,微软将推荐使用此框架,并会有所改善.而且,现在基本上所有数据库均提供了对EF的支 ...

  6. Entity Framework 学习初级篇2--ObjectContext类的介绍

    转自:http://www.cnblogs.com/Tally/archive/2012/09/14/2685014.html 本节,简单的介绍EF中的ObjectContext.ObjectQuer ...

  7. Entity Framework 学习初级篇2--ObjectContext、ObjectQuery、ObjectStateEntry、ObjectStateManager类的介绍

    本节,简单的介绍EF中的ObjectContext.ObjectQuery.ObjectStateEntry.ObjectStateManager这个几个比较重要的类,它们都位于System.Data ...

  8. Entity Framework 学习初级篇5--ObjectQuery查询及方法

    ObjectQuery 类支持对 实体数据模型 (EDM) 执行 LINQ to Entities 和 Entity SQL 查询.ObjectQuery 还实现了一组查询生成器方法,这些方法可用于按 ...

  9. Entity Framework 学习初级篇3-- LINQ TO Entities

    LINQ 技术(即 LINQ to Entities)使开发人员能够通过使用 LINQ 表达式和 LINQ 标准查询运算符,直接从开发环境中针对 实体框架对象上下文创建灵活的强类型查询.LINQ to ...

随机推荐

  1. BJFU 1397 致我们终将逝去的爱情

      LIS 由于要记录轨迹,所以不能用O(nlogn)优化,直接dp加father记录每个节点的转移. #include<cstdio> #include<algorithm> ...

  2. jquery1.8.3和1.11.3的用法区别

    学习js啦dom的对象 发现好复杂 不够简洁 所以我就用法强大的jquery来做功课 突然就遇到attr的方法对复选属性的checked取不了值 给我返回undefined 我查好久都没查出问题 结果 ...

  3. Python 数据分析包:pandas 基础

    pandas 是基于 Numpy 构建的含有更高级数据结构和工具的数据分析包 类似于 Numpy 的核心是 ndarray,pandas 也是围绕着 Series 和 DataFrame 两个核心数据 ...

  4. Activity LauchMode启动模式(转载)

    转载于:http://www.cnblogs.com/plokmju/p/android_ActivityLauncherMode.html 在一个Android应用中,不可避免的会包含多个Activ ...

  5. spring 框架的 @Autowired 和 @Resource 两种注解的区别

    最开始做项目时,依赖注入用到的注解都是 J2EE 的 @Resource,那时还根本不了解 spring 有 @Autowired.心塞. 前两天想到估计有很多刚开始学习 java 的童鞋可能对这两个 ...

  6. C# 16位的GUDI

    引用:  http://www.cnblogs.com/lcwzj/archive/2009/04/16/1436992.html 当我们想要获得一个唯一的key的时候,通常会想到GUID.这个key ...

  7. LeetCode OJ 96. Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  8. zend笔记

    ZEND_STRL(str)  等价于 (str), (sizeof(str)-1) ZEND_STRS(str)等价于 (str), (sizeof(str))

  9. Hibernate Tools for Eclipse安装

    声明:本文转载自 http://developer.51cto.com/art/200906/128067.htm Hibernate Tools for Eclipse Plugins 的安装和使用 ...

  10. listctrl中的cell如何支持被复制

    为了方便测试data pipeline, 使用wxpython开发了一个小工具,用来显示csv文档中的特定列,及数据库中的指定值. 显示数据的contrl选择了listctrl.但这里有个问题,显示的 ...