为什么这个LINQ语句会抛出超时错误?

时间:2013-11-29 13:53:56

标签: c# linq

当我执行此行时,我收到此错误,通过断点我检测到此错误。

  

超时已过期。操作完成之前经过的超时时间或服务器没有响应。

int? dressSerialNo;
var lstDress = (
    from yy in currContext.OrderDressings
    where yy.OrderID == this.OrderID
        && yy.OrderItemID == this.orderItemID
        && yy.ProductID == this.ProductID
    select yy
    ).ToList();
if (lstDress.Count > 0)
{
    dressSerialNo = (
        from yy in lstDress
        where yy.OrderID == this.OrderID
            && yy.OrderItemID == this.orderItemID
            && yy.ProductID == this.ProductID
        select (int?)yy.SrNo
        ).Max();
    dressSerialNo += dressSerialNo + 1;
}
else dressSerialNo = 1;

解决方案: - 在我的项目中,我使用ado.net为一些模块旧方法维护两个事务处理,对于新开发的模块,我使用实体框架,因此它在事务中创建了问题。所以它变得异常。

5 个答案:

答案 0 :(得分:2)

您正在使用Linq-To-Entities。与数据库服务器的连接存在问题。造成这种情况的常见原因是:

  • 查询所花费的时间超过了上下文中指定的超时时间。
  • 网络相关问题导致延迟。

您可以选择更改命令超时(有关如何执行此操作,请参阅this question。)

答案 1 :(得分:2)

您不希望使用.ToList()实现您的所有实体。

您可以编写一个只返回您感兴趣的查询:

// Get the entity that contains the max value, using Ordering
var myMaxIfAny = currContext.OrderDressings
    .Where(yy => yy.OrderID == this.OrderID && yy.OrderItemID == this.orderItemID && yy.ProductID == this.ProductID)
    .OrderByDescending(z => z.SrNo)
    .FirstOrDefault();

if (myMaxIfAny != null)
{
    // Then you got a value, retrieve the Max using myMaxIfAny.SrNo
    // ...
}
else
{
    // ...
}

答案 2 :(得分:2)

我已对您的代码进行了格式化和评论:

int? dressSerialNo;

// Get all OrderDressings with matching OrderID, orderItemID and ProductID as a List<OrderDressing>
var lstDress = (from yy in currContext.OrderDressings 
                where yy.OrderID == this.OrderID 
                   && yy.OrderItemID == this.orderItemID 
                   && yy.ProductID == this.ProductID 
                select yy)
                .ToList();

                // If any were found,               
                if (lstDress.Count > 0)
                {
                    // Execute the Where again (what else will the list contain?) and select all yy.SrNo
                    dressSerialNo = (from yy in lstDress 
                                     where yy.OrderID == this.OrderID 
                                        && yy.OrderItemID == this.orderItemID 
                                        && yy.ProductID == this.ProductID 
                                    select (int?)yy.SrNo)
                                    .Max();     // And take the Max() of that

                    // Add dressSerialNo + 1 to dressSerialNo.
                    dressSerialNo += dressSerialNo + 1;
                }

                else dressSerialNo = 1;

这似乎可以纠正并简化为:

int? serialNumber = (from yy in currContext.OrderDressings 
                     where yy.OrderID == this.OrderID 
                        && yy.OrderItemID == this.orderItemID 
                        && yy.ProductID == this.ProductID 
                     select yy.SrNo)
                     .DefaultIfEmpty() // Might not be necessary
                     .Max();

if (!serialNumber.HasValue)
{
    serialNumber = 1;
}
else
{
    serialNumber++;
}

请注意,如果两个人同时执行此操作,则会导致并发问题。

答案 3 :(得分:1)

对数据库的查询耗时太长。关于为什么会发生这种情况的原因有很多。

尝试将linq生成的sql语句直接运行到数据库,看看它是否需要很长时间。

检查并查看您的任何列是否包含大量数据。 (就像填充大量数据的字符串列一样)

同时尝试将此添加到连接字符串的末尾

Connection Timeout=30000;

答案 4 :(得分:0)

我发现问题是创建超时问题, 在我的应用程序事务中保持2种风格, 一个具有旧的ado.net风格,另一个具有EF风格, 所以它造成了混乱。我要在实体框架中改变所有内容。

相关问题