LINQ 和生成的 sql
假设我的 LINQ 查询就像
var qry = from c in nwEntitiesContext.CategorySet.AsEnumerable()
let products = this.GetProducts().WithCategoryID(c.CategoryID)
select new Model.Category
{
ID = c.CategoryID,
Name = c.CategoryName,
Products = new Model.LazyList<Core.Model.Product>(products)
};
return qry.AsQueryable();
我只想知道它将在运行时生成什么查询....当我们在调试模式下运行代码时如何查看它从 VS2010 IDE 生成什么查询....一步一步指导我。
suppose my LINQ query is like
var qry = from c in nwEntitiesContext.CategorySet.AsEnumerable()
let products = this.GetProducts().WithCategoryID(c.CategoryID)
select new Model.Category
{
ID = c.CategoryID,
Name = c.CategoryName,
Products = new Model.LazyList<Core.Model.Product>(products)
};
return qry.AsQueryable();
i just want to know what query it will generate at runtime....how to see what query it is generating from VS2010 IDE when we run the code in debug mode....guide me step by step.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里没什么可看的 - 它只会从
Category
表中选择所有字段,因为您调用AsEnumerable
从而从Category
中获取所有数据> 将表存入内存。之后您就处于对象空间中。好吧,这取决于this.GetProducts()
的作用 - 我猜它会导致另一个 EF 查询将结果提取到内存中。如果是这种情况,我强烈建议您使用此代码和您的GetProducts
方法的代码发布另一个问题,以便我们可以查看并以更优化的方式重写它。 (除此之外,您正在投影到映射实体Model.Category
上,该实体同样不会(也不应该)与 Linq-to-Entities 一起使用。)在阅读您的查询之前,我打算建议这样做:
但这不会起作用,因为您将 Linq-to-Entities 与 Linq-to-objects 混合在一起,并且在
GetProducts
查询 EF 的情况下实际上会执行多个查询。您可以将这部分与 EF 查询分开,并查看如下 SQL:但正如我之前提到的 - 这只会从
Categories
表中选择所有内容。在您的情况下(除非您以激烈的方式重写代码),您实际上希望查看当您执行代码并枚举查询结果时针对数据库运行了哪些查询。看这个问题:
实体框架执行的精确sql查询
您的选择是
SQL Server Profiler
和 实体框架探查器。您还可以尝试LinqPad
,但总的来说,我仍然建议您更详细地描述您的查询正在执行的操作(并且很可能在继续之前以更优化的方式重写它们)。There is not much to see here - it will just select all fields from the
Category
table since you callAsEnumerable
thus fetching all the data from theCategory
table into memory. After that you are in object space. Well, depending on whatthis.GetProducts()
does - and my guess it makes another EF query fetching the results into memory. If that's the case, I would strongly recommend you to post another question with this code and the code of yourGetProducts
method so that we can take a look and rewrite this in a more optimal way. (Apart from this, you are projecting onto a mapped entityModel.Category
which again won't (and should not) work with Linq-to-Entities.)Before reading into your query I was going to recommend doing something like this:
But that won't work since you are mixing Linq-to-Entities with Linq-to-objects and you will actually have several queries executed in case
GetProducts
queries EF. You can separate the part with your EF query and see the SQL like this though:but as I mentioned earlier - that would just select everything from the
Categories
table.In your case (unless you rewrite your code in a drastic way), you actually want to see what queries are run against the DB when you execute the code and enumerate the results of the queries. See this question:
exact sql query executed by Entity Framework
Your choices are
SQL Server Profiler
and Entity Framework Profiler. You can also try outLinqPad
, but in general I still recommend you to describe what your queries are doing in more detail (and most probably rewrite them in a more optimal way before proceeding).尝试 Linqpad
Try Linqpad
这将产生
SELECT * FROM Category
。而已。一旦调用AsEnumerable
,您就处于 Linq-to-objects 中,并且无法返回到 Linq-to-entities(AsQueryable
不会这样做)。如果您想查看生成的查询,请使用 SQL Profiler 或本文中描述的任何方法。
This will produce
SELECT * FROM Categories
. Nothing more. Once you callAsEnumerable
you are in Linq-to-objects and there is no way to get back to Linq-to-entities (AsQueryable
doesn't do that).If you want to see what query is generated use SQL Profiler or any method described in this article.