How to loop through List<dynamic>

17,028
    foreach (var v in (i as IEnumerable<object>))
    {
        if (v is KeyValuePair<string, string>)
        {
            // Do stuff
        }
        else if (v is List<string>)
        {
            //Do stuff
        }

        else throw new InvalidOperationException();
    }
Share:
17,028

Related videos on Youtube

user3763117
Author by

user3763117

Updated on June 06, 2022

Comments

  • user3763117
    user3763117 almost 2 years

    I have a Class which contains a dynamic List, which I manage to access like this.

    foreach (var c in objects)
    {
        foreach (dynamic i in c.data)
        {
            var ss = i;
        }
    }
    

    The Class

    public class iUpdateGrid
    {
        public int type { get; set; }
        public string session { get; set; }
        public string latitude { get; set; }
        public string longitude { get; set; }
        public string ip { get; set; }
        public int id { get; set; }
        public string station { get; set; }
        public List<iGridData> h_33 { get; set; }
    }
    public class iGridData
    {
        public string table { get; set; }
        public List<dynamic> data { get; set; }
    }
    

    "ss" contains now a list of objects, however have no idea how to get these values to a Dictionary or list. One note I also need to adjust the key names.

    Things I tried to do:

    foreach (KeyValuePair<string, string> kv in i)
    {
        string key = kv.Key;
        string value = kv.Value;
    }
    

    foreach statement cannot operate on variables of type 'System.Collections.IEnumerable' because 'System.Collections.IEnumerable' does not contain a public definition for 'GetEnumerator'

    Also the Dynamic objects does not give me the options to access the key and/or value.

    Any help how I can loop through this would be very welcome.

    also when I put this on runtime "i" says its a System.Collections.Generic.Dictionary however can access this also.

    Solution:

    Made little chance to solution as provided, but this worked for me:

            foreach (var c in objects)
            {
                foreach (dynamic i in c.data)
                {
                    foreach (var v in (i as IDictionary<string, object>))
                    {
                        string key = v.Key;
                        object value = v.Value;
                    }
                }
            }
    
    • Eren Ersönmez
      Eren Ersönmez over 9 years
      the question is "how to I convert c.data to a dictionary", right? If so, what is the type of c.data?
  • user3763117
    user3763117 over 9 years
    I get error "Object reference not set to an instance of an object." on (i as IEnumerable<object>)
  • Donald Duck
    Donald Duck about 7 years
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
  • Shakiba Moshiri
    Shakiba Moshiri about 7 years
    Please describe your answer and not just using code. Otherwise may your answer will be deleted!