Entity Framework學習初級篇4--Entity SQL

iDotNetSpace發表於2009-05-13

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] 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 的支援。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-594966/,如需轉載,請註明出處,否則將追究法律責任。

相關文章