How can I use listDictionary?

I can fill out my listdictinary, but if the startup error returns to me in "foreach (string ky in ld.Keys)" (invalid operation The exception was unhandled)

Error Details: After creating a pointer to a list, the collection of samples was changed. WITH#

       ListDictionary ld = new ListDictionary();
            foreach (DataColumn dc in dTable.Columns)
            {
                MessageBox.Show(dTable.Rows[0][dc].ToString());
                ld.Add(dc.ColumnName, dTable.Rows[0][dc].ToString());
            }

            foreach (string ky in ld.Keys)
                if (int.TryParse(ld[ky].ToString(), out QuantityInt))
                    ld[ky] = "integer";
                else if(double.TryParse(ld[ky].ToString(), out QuantityDouble))
                    ld[ky]="double";
                else
                    ld[ky]="nvarchar";
+3
source share
2 answers

Your second foreach loop modifies the ListDictionary by setting ld [ky] = "whatever"; You cannot do this with the foreach loop, because inside it uses an enumerator. When using an enumerator, it is forbidden to modify the enumerated collection.

Use the for loop instead.

, dTable.Columns, .

ListDictionary ld = new ListDictionary();
foreach (DataColumn dc in dTable.Columns)
{
     MessageBox.Show(dTable.Rows[0][dc].ToString());

     string value;
     if (int.TryParse(dTable.Rows[0][dc].ToString(), out QuantityInt))
           value = "integer";
     else if(double.TryParse(dTable.Rows[0][dc].ToString(), out QuantityDouble))
           value="double";
      else
           value="nvarchar";

     ld.Add(dc.ColumnName, value);
}
+2

foreach.
for.

0

Source: https://habr.com/ru/post/1750650/


All Articles