Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert type 'System.Collections.Generic.KeyValuePair<int, dynamic>' to 'System.Collections.Generic.Dictionary<int, dynamic>

I am trying to loop through dictionary of values and then updating row of "MTables" (table Name) fields in 'QvxDataRow MakeEntry()' function through Key values of Dictionary.

i need "MTables" to contain like this

SlNo StudentName StudentClass
1        Daniel                4
2         Maties               4

but in my code i get error for for each loop like below

Cannot convert type 'System.Collections.Generic.KeyValuePair<int, dynamic>' to 'System.Collections.Generic.Dictionary<int, dynamic>

I need to add hardcoded value and put it into row of MTables. I have this

   private IEnumerable<QvxDataRow> GetApplicationDB()
        {
            Dictionary<int, dynamic> map = new Dictionary<int, dynamic>
    {
        {1, new {SlNo="1", StudentName="Daniel",StudentClass="4"}},
        {2, new {SlNo="2", StudentName="Maties",StudentClass="4"}}
    };

            foreach (Dictionary<int, string> evl in map)
            {
                yield return MakeEntry(evl, FindTable("ApplicationsStudentDB", MTables));
            }
}

        private QvxDataRow MakeEntry( evl, QvxTable table)
        {
            var row = new QvxDataRow();
            row[table.Fields[0]] = evl.SlNo;
            row[table.Fields[1]] = evl.StudentName;
            row[table.Fields[2]] = evl.StudentClass;
            return row;
        }

How could i do this, Please help me on this.

like image 514
daisy Avatar asked May 04 '26 01:05

daisy


1 Answers

It's because when you loop through items in a Dictionary, the item within the Dictionary is not a Dictionary, it's a KeyValuePair. You're declaring evl with the wrong type in the loop. Also, your dictionary contains int and dynamic types, but then you declare the loop as a pair of strings. You can't just alter the type like that. It should be:

foreach (KeyValuePair<int, dynamic> evl in map)
like image 195
ADyson Avatar answered May 06 '26 14:05

ADyson