【发布时间】:2014-01-12 05:05:42
【问题描述】:
这才刚刚开始。这个声明已经工作了几个月,现在我只是不断收到下面的超时错误。当我直接在 SSMS 上执行相同的语句时,它会在一秒钟内返回。该表有 44k 条记录,并且是 5 列的索引状态是其中之一。
select distinct(state) from [ZipCodeDatabase]
我正在运行以下 linq 语句
states = ZipCodeRepository.Get(orderBy: z => z.OrderBy(o => o.State)).Select(z => z.State).Distinct().ToList();
当我运行这个 linq 语句时,我不断收到超时错误并且不知道 y,因为它之前工作正常。
我包含了 Get() 函数,它是一个通用的 repo 函数,但也许我在那里遗漏了一些东西,这导致了延迟。
获取函数:
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "") //params Expression<Func<TEntity, object>>[] includes
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
超时错误:
System.Data.SqlClient.SqlException (0x80131904):超时。在操作完成之前超时时间已过或服务器没有响应。 ---> System.ComponentModel.Win32Exception (0x80004005): 等待操作超时 在 System.Data.SqlClient.SqlConnection.OnError(SqlException 异常,布尔型 breakConnection,Action
1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,布尔调用者HasConnectionLock,布尔异步关闭) 在 System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean & dataReady) 在 System.Data.SqlClient.SqlDataReader.TryCloseInternal(布尔 closeReader) 在 System.Data.SqlClient.SqlDataReader.Close() 在 System.Data.Common.DbDataReader.Dispose(布尔处理) 在 System.Data.Common.DbDataReader.Dispose() 在 System.Data.Common.Internal.Materialization.Shaper1.Finally() at System.Data.Common.Internal.Materialization.Shaper1.SimpleEnumerator.Dispose() 在 System.Collections.Generic.List1..ctor(IEnumerable1 集合) 在 System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at ClientsToProfitsKendoUI.DAL.GenericRepository1.Get(Expression1 filter, Func2 orderBy, String includeProperties)
【问题讨论】:
-
这通常意味着 SQL 中发生了类型转换。当
datetime被转换为nvarchar时,我已经看到了这种情况。通过代码发送查询窗口中需要 -
您实现所有 44K 记录只是为了获取状态?
标签: c# sql linq sql-server-2008 entity-framework