【发布时间】:2011-09-04 21:11:03
【问题描述】:
可能重复:
Collection was modified; enumeration operation may not execute
你好,
我正在创建一个项目估算程序并收到以下错误:C# Collection was modified;枚举操作可能无法执行。
它与使用此有关: 我首先在全球范围内声明字典:
Dictionary<int, int> rankings = new Dictionary<int, int>();
包含此字典的 Next 方法执行以下操作:
private void getFirstEstimation()
{
List<int> array = new List<int>();
string strConnection = ConfigurationSettings.AppSettings["ConnectionString"];
MySqlConnection connection = new MySqlConnection(strConnection);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader reader;
command.CommandText = "SELECT idprojects FROM `test`.`projects` WHERE application_layers = " + applicationTiers;
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
array.Add(Convert.ToInt32(reader["idprojects"].ToString()));
}
foreach (int i in array)
{
rankings[i] = 15;
}
connection.Close();
}
我在这里第二次调用它:
private void getSecondEstimation()
{
Dictionary<int, string> sqltext = new Dictionary<int, string>();
Dictionary<int, int> valueForSql = new Dictionary<int, int>();
Dictionary<int, int> weightings = new Dictionary<int, int>();
sqltext.Add(1, "project_type");
valueForSql.Add(1, projectType);
weightings.Add(1, 10);
sqltext.Add(2, "application_domain");
valueForSql.Add(2, applicationDomain);
weightings.Add(2, 8);
sqltext.Add(3, "organisation_size");
valueForSql.Add(3, organizationSize);
weightings.Add(3, 8);
sqltext.Add(4, "no_of_locations");
valueForSql.Add(4, noOfLocations);
weightings.Add(4, 7);
sqltext.Add(5, "development_process");
valueForSql.Add(5, developmentProcess);
weightings.Add(5, 6);
sqltext.Add(6, "rules_engine");
valueForSql.Add(6, rulesEngine);
weightings.Add(6, 5);
sqltext.Add(7, "middleware");
valueForSql.Add(7, middleware);
weightings.Add(7, 4);
sqltext.Add(8, "location_of_development");
valueForSql.Add(8, locationOfDevelopment);
weightings.Add(8, 3);
sqltext.Add(9, "programming_language");
valueForSql.Add(9, programmingLanguage);
weightings.Add(9, 3);
sqltext.Add(10, "development_environment");
valueForSql.Add(10, developmentEnvironment);
weightings.Add(10, 3);
sqltext.Add(11, "backend");
valueForSql.Add(11, backend);
weightings.Add(11, 3);
sqltext.Add(12, "webserver");
valueForSql.Add(12, webServer);
weightings.Add(12, 3);
List<int> array = new List<int>();
string strConnection = ConfigurationSettings.AppSettings["ConnectionString"];
MySqlConnection connection = new MySqlConnection(strConnection);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader reader;
for (int i = 1; i <= 12; i++)
{
command.CommandText = "SELECT idprojects FROM `test`.`projects` WHERE " + sqltext[i] + " = " + valueForSql[i];
connection.Open();
//int testInt;
reader = command.ExecuteReader();
while (reader.Read())
{
array.Add(Convert.ToInt32(reader["idprojects"].ToString()));
}
foreach (int a in array)
{
if (!rankings.ContainsKey(a))
{
rankings[a] = 0;
}
rankings[a] = rankings[a] + weightings[i];
}
connection.Close();
}
}
问题出现在代码的这个区域:
private void getThirdEstimation()
{
ArrayList tempModuleHolder;
string strConnection = ConfigurationSettings.AppSettings["ConnectionString"];
MySqlConnection connection = new MySqlConnection(strConnection);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader reader;
int similarModules;
foreach (KeyValuePair<int, int> kvp in rankings)
{
similarModules = 0;
tempModuleHolder = new ArrayList();
command.CommandText = "SELECT id_modules FROM `test`.`modules_in_project` WHERE id_project = " + kvp.Key;
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
tempModuleHolder.Add(Convert.ToInt32(reader["id_modules"].ToString()));
}
foreach (int i in tempModuleHolder)
{
if(modules.Contains(i))
{
similarModules++;
}
}
if((double)(similarModules/modules.Count)>0.6)
{
//kvp.Value = kvp.Value + 4;
rankings[kvp.Key] = rankings[kvp.Key] + 4;
}
connection.Close();
}
}
对于问题所在的任何帮助将不胜感激
【问题讨论】:
-
顺便说一句,题外话,你的 MySqlConnection 和 MySqlDataReader 类没有实现 IDisposable 到 Dispose() 方法,所以你可以在连接和阅读器周围使用 using 块?
-
@ydobonmai:
MySqlConnection和MySqlDataReader可能是指MySql.Data.MySqlClient使用 MySQL 数据库的类。因此,它们确实实现了IDisposable,并且它们的使用应该包装在using块中。
标签: c# collections dictionary