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] 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 的支援。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-594966/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Entity Framework 學習初級篇3-- LINQ TO EntitiesFramework
- Entity Framework 學習初級篇5--ObjectQuery查詢及方法FrameworkObject
- Entity Framework 學習初級篇2--ObjectContext類的介紹FrameworkObjectContext
- Entity Framework(實體框架)之Entity SQLFramework框架SQL
- Entity Framework Code First學習系列目錄Framework
- Entity Framework Tutorial Basics(8):Types of Entity in Entity FrameworkFramework
- Java工程師學習指南(初級篇)Java工程師
- Java工程師學習指南 初級篇Java工程師
- Entity Framework:Code-First Tutorial開篇Framework
- Entity Framework Tutorial Basics(20):Persistence in Entity FrameworkFramework
- Entity Framework Tutorial Basics(2):What is Entity Framework?Framework
- Entity Framework Tutorial Basics(39):Raw SQL QueryFrameworkSQL
- Entity Framework Tutorial Basics(29):Stored Procedure in Entity FrameworkFramework
- Entity Framework Tutorial Basics(3):Entity Framework ArchitectureFramework
- Entity Framework Tutorial Basics(4):Setup Entity Framework EnvironmentFramework
- 初試 Entity Framework Core 的多對多對映Framework
- Entity Framework Code-First(23):Entity Framework Power ToolsFramework
- Entity Framework(1)Framework
- entity framework in mysqlFrameworkMySql
- Entity Framework 教程Framework
- .NET Core學習筆記(8)——Entity Framework Core之Database First筆記FrameworkDatabase
- .NET Core學習筆記(9)——Entity Framework Core之Code First筆記Framework
- 學習Grafana初級使用Grafana
- Entity Framework Tutorial Basics(33):Spatial Data type support in Entity Framework 5.0Framework
- Entity Framework Tutorial Basics(40):Validate EntityFramework
- Entity Framework Tutorial Basics(42):Colored EntityFramework
- Entity Framework Tutorial Basics(10):Entity LifecycleFramework
- Entity Framework Tutorial Basics(27):Update Entity GraphFramework
- Entity Framework Tutorial Basics(9):Entity RelationshipsFramework
- 學習SQL 快取依賴的筆記(比較初級)SQL快取筆記
- Entity Framework Tutorial Basics(24):Update Single EntityFramework
- Entity Framework Tutorial Basics(25):Delete Single EntityFrameworkdelete
- Entity Framework Tutorial Basics(26):Add Entity GraphFramework
- Entity Framework Tutorial Basics(5):Create Entity Data ModelFramework
- 10、Entity Framework Core 3.1入門教程-執行原生SQLFrameworkSQL
- AppBox升級進行時 - Attach陷阱(Entity Framework)APPFramework
- Entity Framework+SQLite+DataBaseFirstFrameworkSQLiteDatabase
- Entity Framework Tutorial Basics(30):Framework