2012-04-18 70 views
1

我遇到了兩天內無法解決的問題。這是故事。
我從我的MySQL 5.0數據庫使用LINQ to SQL獲取的記錄列表,但執行將異常說破:超時已過期使用linq將數據提取到sql時出現異常

超時過期。操作完成之前超時的時間或服務器沒有響應。

我在後端首先使用了實體框架工作代碼。這裏是我的代碼:

context = new NebeContext(); 
var data2 = (from row in context.PollingStationConstituencyCandidates 
    where row.ElectionId.Equals(electionId) && row.ConstituencyId.Equals(constituencyId) 
    select new { row.LocationId, row.LocationNameA, row.PollingStationNameA, row.PollingStationNameE, row.PollingStationId }).Distinct(); 

List<PollingStationInfo> resultList = new List<PollingStationInfo>(); 
PollingStationInfo result = new PollingStationInfo(); 
foreach (var i in data2) 
{ 
    result = new PollingStationInfo() { LocationId = i.LocationId, LocationNameA = i.LocationNameA, NameA = i.PollingStationNameA, NameE = i.PollingStationNameE, PollingStationId = i.PollingStationId }; 
    resultList.Add(result); 
} 
return resultList; 

回答

1

好了,該錯誤信息是自解釋:你的查詢時間過長數據庫來完成,你會得到一個超時...

或者:

剖析sql,看看你是否需要一些額外的索引或更好的查詢。另外,請檢查你沒有得到N + 1查詢上的datacontext

更改逾