LINQ 和生成的 sql

发布于 2024-11-04 05:35:28 字数 529 浏览 0 评论 0原文

假设我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

香橙ぽ 2024-11-11 05:35:28

这里没什么可看的 - 它只会从 Category 表中选择所有字段,因为您调用 AsEnumerable 从而从 Category 中获取所有数据> 将表存入内存。之后您就处于对象空间中。好吧,这取决于 this.GetProducts() 的作用 - 我猜它会导致另一个 EF 查询将结果提取到内存中。如果是这种情况,我强烈建议您使用此代码和您的 GetProducts 方法的代码发布另一个问题,以便我们可以查看并以更优化的方式重写它。 (除此之外,您正在投影到映射实体 Model.Category 上,该实体同样不会(也不应该)与 Linq-to-Entities 一起使用。)

在阅读您的查询之前,我打算建议这样做:

string sqlQueryString = ((ObjectQuery)qry).ToTraceString();

但这不会起作用,因为您将 Linq-to-Entities 与 Linq-to-objects 混合在一起,并且在 GetProducts 查询 EF 的情况下实际上会执行多个查询。您可以将这部分与 EF 查询分开,并查看如下 SQL:

string sqlString = nwEntitiesContext.CategorySet.ToTraceString();

但正如我之前提到的 - 这只会从 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 call AsEnumerable thus fetching all the data from the Category table into memory. After that you are in object space. Well, depending on what this.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 your GetProducts 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 entity Model.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:

string sqlQueryString = ((ObjectQuery)qry).ToTraceString();

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:

string sqlString = nwEntitiesContext.CategorySet.ToTraceString();

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 out LinqPad, 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).

子栖 2024-11-11 05:35:28

尝试 Linqpad

素年丶 2024-11-11 05:35:28

这将产生SELECT * FROM Category。而已。一旦调用 AsEnumerable,您就处于 Linq-to-objects 中,并且无法返回到 Linq-to-entities(AsQueryable 不会这样做)。

如果您想查看生成的查询,请使用 SQL Profiler 或本文中描述的任何方法

This will produce SELECT * FROM Categories. Nothing more. Once you call AsEnumerable 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文