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 的支持。

 
     本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    转自: ♂风车车.Net
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Entity Framework 来操作 SQL Server 数据库。Entity Framework 是一个对象关系映射(ORM)框架,它可以帮助你通过代码来操作数据库,而不需要直接编写 SQL 查询语句。 首先,你需要安装 Entity Framework。你可以在 Visual Studio 的 NuGet 包管理器中搜索并安装 Entity Framework 相关的包。 接下来,你需要定义一个数据上下文(DbContext)类,该类表示数据库中的一组实体(表)。你可以在数据上下文类中定义 DbSet 属性,每个 DbSet 属性对应一个实体的集合。 然后,你可以使用 LINQ 查询语法来查询和操作数据库。Entity Framework 会将 LINQ 查询转换为相应的 SQL 查询语句,并执行在数据库中。 下面是一个简单的示例: ```csharp // 创建数据上下文类 public class MyDbContext : DbContext { public DbSet<Customer> Customers { get; set; } // 其他实体的 DbSet 属性 } // 定义实体类 public class Customer { public int Id { get; set; } public string Name { get; set; } // 其他属性 } // 使用数据上下文类操作数据库 using (var context = new MyDbContext()) { // 查询数据 var customers = context.Customers.Where(c => c.Name.StartsWith("John")).ToList(); // 插入数据 var customer = new Customer { Name = "John Smith" }; context.Customers.Add(customer); context.SaveChanges(); // 更新数据 customer.Name = "John Doe"; context.SaveChanges(); // 删除数据 context.Customers.Remove(customer); context.SaveChanges(); } ``` 这只是一个简单的示例,你可以根据具体的需求进一步学习和使用 Entity Framework 操作 SQL Server 数据库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值