我收到System.InvalidOperationException:收集被修改; 枚举操作可能无法执行.我的以下代码中的错误.
//temporary var for storing column sort orders according to view type
Dictionary<string, bool?> tempColumnSortOrders=new Dictionary<string,bool?>(4);
//Check for column name in col list
if (tempColumnSortOrders.ContainsKey(fieldToSort))
{
//If exists set column sort order to new sort order
//Set new sort order
tempColumnSortOrders[fieldToSort] = sortOrder;
var tempSortOrders = tempColumnSortOrders;
//remove sort order of other columns
foreach (var kvp in tempSortOrders)
{
//Reset other columns sort other than current column sort
if (kvp.Key != fieldToSort)
{
tempSortOrders[kvp.Key] = null;
}
}
//Return name of column to sort
return fieldToSort;
}
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪
[InvalidOperationException:集合已被修改; 枚举操作可能无法执行.]
System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)+52 System.Collections.Generic.Enumerator.MoveNext()+44 GlaziersCenter.Handlers.GetSiteViews.getColumnToSort(Int32 viewType)在d:\ Projects\GlaziersCenter\GlaziersCenter\Handlers\GetSiteViews.ashx.cs:184 GlaziersCenter.Handlers.GetSiteViews.ProcessRequest(HttpContext context)在d:\ Projects\GlaziersCenter\GlaziersCenter\Handlers\GetSiteViews.ashx.cs:68 System.Web.CallHandlerExecutionStep.System.Web .HttpApplication.IExecutionStep.Execute()+ 341 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&completedSynchronously)+69
请尝试使用此代码,
List<string> keys = new List<string>(tempSortOrders.Keys);
foreach (var key in keys)
{
//Reset other columns sort other than current column sort
if (key != fieldToSort)
{
tempSortOrders[key] = null;
}
}
Run Code Online (Sandbox Code Playgroud)
更新,
将集合转换为列表将解决问题.
| 归档时间: |
|
| 查看次数: |
5993 次 |
| 最近记录: |