可能重复:
sql timeout expired
我有一些Linq代码,我不太确定它在做什么。基本上我是从一些表中提取报告。这个linq语句试图返回227106条记录,大约需要1.45分钟。当我从代码执行它时,它给了我一个'Timeout'异常:
An error occurred while executing the command definition. See the inner exception for details.
at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
at System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at #####.Service.TrackingLogService.GetPastDayClickingReports(Guid AffiliateID, Int32 pastDays) in ...
代码如下:
var temp = (from l in logs
group l by new { l.AffiliateGUIDID, l.MarketingTool, l.RefereceURL } into g
select new ClickingReportItem
{
Name = g.Key.MarketingTool.Title,
MediaType = g.Key.MarketingTool.MediaType.Name,
RefereceURL = g.Key.RefereceURL,
Impressions = (g.Key.MarketingTool.ImpressionTracking.HasValue ? g.Key.MarketingTool.ImpressionTracking.Value : false)
? g.Count(m => m.TrackingTypeID == (int)Contract.TrackingType.Impressions) : (int?)null,
Clicks = g.Count(m => m.TrackingTypeID == (int)Contract.TrackingType.Clicks),
Registrations = g.Count(m => m.TrackingTypeID == (int)Contract.TrackingType.Registrations),
Purchases = 0,
Commissions = 0
}).OrderByDescending(m => new { m.Registrations,m.Clicks,m.Impressions}).AsQueryable();
return temp.ToList();
从几个帖子/博客我尝试将Connect Timeout = 0添加到我的连接字符串,尝试连接超时= 1800无效。
一些建议的context.CommandTimeout = 240,但我不确定CommandTimeout可以在我的代码中应用的位置。
我在Linq不是很好。
由于
ClayCass
答案 0 :(得分:0)
如果您使用的是DataContext,则使用CommandTimeout属性设置超时。
http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.commandtimeout.aspx