与类似实体框架查询的不同结果

发布于 2025-02-01 12:10:00 字数 1129 浏览 5 评论 0原文

我这里有一个奇怪的情况,我只是想更好地理解。我在更新时要维护的项目中使用实体框架6,并且我在此查询中浏览了导致超时异常的查询。

基础结构:

  1. [orders] - 包含订单相关数据的表。

  2. [searchorders] - 包含ID列和可搜索文本列的视图。

  3. 一个表值函数定义为:

     选择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:

  1. [Orders] - a table containing order-related data.

  2. [SearchOrders] - a view containing an Id column and a searchable text column.

  3. 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文