与类似实体框架查询的不同结果
我这里有一个奇怪的情况,我只是想更好地理解。我在更新时要维护的项目中使用实体框架6,并且我在此查询中浏览了导致超时异常的查询。
基础结构:
[orders]
- 包含订单相关数据的表。[searchorders]
- 包含ID列和可搜索文本列的视图。一个表值函数定义为:
选择r。[键] 从contablestable(搜索词, *,@searchphrase)作为r
环境为:ASP.NET MVC 5具有SQL Server数据库的环境。
我有一个iQueryable< order
存储在变量中的对象,称其为resulte
。
如果执行以下行,我会等待几秒钟并接收一个SQL超时例外:
var orderIdsToReturn = results.Select(x => x.Id).ToArray();
return db.Orders.Where(x => orderIdsToReturn.Contains(x.Id));
但是,如果我执行此代码,则实现所需的输出:
var ordersToReturn = results.ToArray();
var orderIdsToReturn = ordersToReturn.Select(x => x.Id);
return db.Orders.Where(x => x.orderIdsToReturn.Contains(x.Id));
在两种情况下,我都有一个iqueryable< order> 对象,然后将ID属性选择为整数数组,然后使用该整数数组返回实际对象。返回线是无关紧要的,但是我将它们包括在示例中以进行完整。
SQL超时异常发生在第一个示例的第一行。
我不确定与“搜索词”(查看)或表值函数有关的情况是否有任何关系; 对象,尽管我觉得一些基本问题可能是由源order
元素以及它们如何在引擎盖下映射的。
I have a strange situation here that I'm just looking to understand a little better. I'm using Entity Framework 6 in a project that I am maintaining as I update, and I cam across this query that is causing a timeout exception.
The underlying structure:
[Orders]
- a table containing order-related data.[SearchOrders]
- a view containing an Id column and a searchable text column.A table-valued function defined as:
select r.[key] from containstable(SearchOrders, *, @searchPhrase) as r
The environment is ASP.NET MVC 5 with a SQL Server database.
I have an IQueryable<Order>
object stored in a variable, call it results
.
If I execute the following line, I wait for a few seconds and receive a SQL timeout exception:
var orderIdsToReturn = results.Select(x => x.Id).ToArray();
return db.Orders.Where(x => orderIdsToReturn.Contains(x.Id));
However, if I execute this code, the desired output is realized:
var ordersToReturn = results.ToArray();
var orderIdsToReturn = ordersToReturn.Select(x => x.Id);
return db.Orders.Where(x => x.orderIdsToReturn.Contains(x.Id));
In both situations, I have an IQueryable<Order>
object, which is then selecting the ID properties into an integer array, and then using that integer array to return the actual objects. The return lines are irrelevant, but I included them in the example for completeness.
The SQL timeout exception is occurring on the first line of the first example.
I'm not sure if there is any relevance to the "SearchOrders" (View) or the Table-Valued Function in regards to the situation because, at the time that the exception occurs I am only dealing with the IQueryable<Order>
object, though I feel like some of the underlying issue may be caused by the source Order
elements and how they are mapped underneath the hood.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论