230635351.png

由此可以看出其最终都转移成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);
}