由此可以看出其最終都轉移成Command Tree 然後再轉換成對應數據庫的T-SQL語句,本質差別不大 ,但是有時執行特殊查詢語句的時候還是有點不一樣的,因為Entity SQL的T-SQL語句是我們自己定義的,而 LINQ to Entity最後轉換的T-SQL語句是由Entity引擎轉換的,有時我們用SQL Server Profiler檢測執行的 SQL語句的時候LINQ to Entity執行的SQL往往讓我們不容易理解,所以在需要對某些查詢/修改/更新執行操作 的時候如果發現執行效率不高的話可以考慮使用Entity SQL自定義執行SQL語句,下邊貼出點代碼:
1.LINQ TO Entity
string city = "London"; using (Entities entities = new Entities()) { var query = from c in entities.Customers where c.Address.City == city select c; foreach (Customers c in query) Console.WriteLine(c.CompanyName); }
2.Entity SQL(注意SQL語句哦)
string city = "London"; using (Entities entities = new Entities()) { ObjectQuery<Customers> query = entities.CreateQuery<Customers>( "SELECT VALUE c FROM Customers AS c WHERE c.Address.City = @city",new ObjectParameter ("city", city) ); foreach (Customers c in query) Console.WriteLine(c.CompanyName); }