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。如果您只是希望以非確定的方式限制結果中的行數,則應使用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;
例如:
stringesql ="using System; select cast(p.UnitPrice as Int32) from NorthwindEntities.Products as p order by p.ProductID limit 10 ";
stringesql ="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的支持。