实体框架对象LINQ查询超时问题

时间:2017-06-22 10:08:13

标签: c# entity-framework linq timeout executiontimeout

我试图修改我的连接字符串以包含扩展超时,并且我已经确认在sql server端,提供给我的EF对象的视图在几秒钟内执行并返回总共3000或更少的记录。

但是当我尝试通过代码运行它时,我现在遇到了Timeout问题,我正在寻求一些建议来解决这个问题。我得到“Execution Timeout Expired。超时时间在操作完成之前已经过去,或者服务器没有响应。” 我在特定错误上找到的大多数解决方案都建议使用连接字符串修改或者沿着this.context.CommandTimeout ...我无法弄清楚如何在这种情况下使用。

我已经包含了用于获取所需数据的方法。如果有更有效的方式,请告诉我。

输入参数是:

  • int? inputSKU = null
  • int? inputStoreNum = null
  • DateTime? inputStartDate = null

目的是返回完整列表。

它会挂起,因为它会跳过所有条件位: var qUniqueOffers = query.GroupBy(q => q.Plan_Number).ToList();

谢谢。

private List<PromotionItem> QueryPromotion(int? inputSKU, int? inputStoreNum, DateTime? inputStartDate)
{
    log.Info("Client requested QueryPromotion");
    List<PromotionItem> resultQuery = new List<PromotionItem>();

    try
    {
        using (DWH_Entities db = new DWH_Entities())
        {
            var query = db.vw_Web_Promotion.AsQueryable();

            // filter promotion results that don't match SKU#
            if (inputSKU != null)
                query = query.Where(q => q.Sku_Number == inputSKU);
            // filter promotion results that don't match Store Num
            if (inputStoreNum != null)
                query = query.Where(q => q.Store_Number == inputStoreNum);
            // filter promotion results that don't match Promotion Start Date
            if (inputStartDate != null)
                query = query.Where(q => q.Start_Date >= inputStartDate);
            // Group promotions By Plan Number ('Promotion ID')
            var qUniqueOffers = query
                                .GroupBy(q => q.Plan_Number)
                                .ToList();
            // Select first from each group to get unique details
            var qOffers = qUniqueOffers
                        .Select(g => g.OrderBy(gi => gi.Plan_Number).First())
                        .ToList();

            foreach (var qo in qOffers)
            {
                resultQuery.Add(new PromotionItem
                {
                    PromotionNumber = qo.Plan_Number.Trim(),
                    PromotionDescription = qo.Plan_Description.Trim(),
                    StartDate = qo.Start_Date,
                    EndDate = qo.End_Date
                });
            }
        }
    }
    catch (Exception e)
    {
        log.Error("[" + e.TargetSite + "] | " + e.Message);
        throw e;
    }

    return resultQuery;
}

2 个答案:

答案 0 :(得分:2)

如果您使用的是最新的EF版本,请执行以下操作以增加超时:

var temp = query.ToList();
var qUniqueOffers = temp.GroupBy(q => q.Plan_Number)
                                .ToList();

如果您想在最短时间内获得记录,请尝试以下操作:

<?php
//We want to force a download box with the filename hello.txt
header('Content-Disposition: attachment;filename=hello.txt');


//File is located at /home/username/hello.txt
header('X-Sendfile: /home/username/hello.txt');

答案 1 :(得分:0)

// Group promotions By Plan Number ('Promotion ID')
var qUniqueOffers = query
                    .GroupBy(q => q.Plan_Number)
                    .ToList();
// Select first from each group to get unique details
var qOffers = qUniqueOffers
            .Select(g => g.OrderBy(gi => gi.Plan_Number).First())
            .ToList();

您编写上述LINQ的方式意味着您通过线路(第一个ToList)提取大量数据,然后获取数据的子集(使用First和第二个{ {1}})。考虑将其更改为:

ToList

这会导致从数据库发送更少的数据 - 这有望使其更快。

正如https://stackoverflow.com/a/13827077/34092所述:

  

ToList()总是强制前面的所有内容立即进行评估,   而不是延期执行。