我有一個基於某些參數的方法在給出一個字符串列表的兩個日期之間找到'Transactions'。當列表大於1000時,我得到一個堆棧溢出異常,試圖迭代列表。LINQ to SQL中的堆棧溢出異常
這裏是我的代碼
public List<string> CodesWithTransactionsBetweenDates(DateTime startInclusive, DateTime endExclusive, List<string> associatedCodes, int marketId)
{
List<string> codesWithTransactionsInPeriod = new List<string>();
using (var context = new MarketPlaceEntities())
{
var transactionList = (from transactions in context.Transactions
where
associatedCodes.Contains(transactions.User.Code) &&
transactions.MarketId == marketId &&
transactions.Date >= startInclusive &&
transactions.Date < endExclusive
group transactions by transactions.User.Code into uniqueIds
select new { UserCode = uniqueIds.Key });
foreach (var transaction in transactionList)
{
codesWithTransactionsInPeriod.Add(transaction.UserCode);
}
return codesWithTransactionsInPeriod;
}
}
這裏是堆棧跟蹤......它會過去的這Visual Studio可以處理點。
System.Data.Entity.dll!System.Data.Query.InternalTrees.BasicOpVisitor.VisitChildren(System.Data.Query.InternalTrees.Node n) + 0x3 bytes
System.Data.Entity.dll!System.Data.Query.PlanCompiler.GroupAggregateRefComputingVisitor.VisitDefault(System.Data.Query.InternalTrees.Node n) + 0x2b bytes
System.Data.Entity.dll!System.Data.Query.InternalTrees.BasicOpVisitor.VisitRelOpDefault(System.Data.Query.InternalTrees.RelOp op, System.Data.Query.InternalTrees.Node n) + 0xe bytes
System.Data.Entity.dll!System.Data.Query.InternalTrees.BasicOpVisitor.VisitApplyOp(System.Data.Query.InternalTrees.ApplyBaseOp op, System.Data.Query.InternalTrees.Node n) + 0xe bytes
System.Data.Entity.dll!System.Data.Query.InternalTrees.BasicOpVisitor.Visit(System.Data.Query.InternalTrees.OuterApplyOp op, System.Data.Query.InternalTrees.Node n) + 0xe bytes
System.Data.Entity.dll!System.Data.Query.InternalTrees.OuterApplyOp.Accept(System.Data.Query.InternalTrees.BasicOpVisitor v, System.Data.Query.InternalTrees.Node n) + 0x10 bytes
System.Data.Entity.dll!System.Data.Query.InternalTrees.BasicOpVisitor.VisitNode(System.Data.Query.InternalTrees.Node n) + 0x14 bytes
System.Data.Entity.dll!System.Data.Query.InternalTrees.BasicOpVisitor.VisitChildren(System.Data.Query.InternalTrees.Node n) + 0x60 bytes
我的問題是什麼是一種方法,我可以處理這個查詢,使我不必擔心堆棧溢出異常?
你使用的是什麼版本的Linq?有一個4.0之前的bug可能會導致這種情況發生:http://damieng.com/blog/2009/06/01/linq-to-sql-changes-in-net-40 *「Contains now now detect self-引用IQueryable並且不會導致堆棧溢出「* – 2013-02-21 20:30:21
@MatthewWatson我正在使用.NET Framework 4.0謝謝:) – 2013-02-21 20:42:57
處理堆棧溢出的一般建議:查看調試器中的調用堆棧以查看涉及哪些方法。 – 2013-02-21 21:20:18