0

I have this object:

Object
 Adress - count = 2 - System.Collections.Generic.List<Address>
  [0]   Address
    City - "Text" - string
    State - "Text" - string
  [1]   Address
    City - "Text" - string
    State - "Text" -string
 Notes - "Text" - string
 Items - Type(Item)
  Item - count = 2 - System.Collections.Generic.List<Item>
   [0]  Item
    Name - "Text" - string
    Price - "Text" - string
   [1]  Item
    Name - "Text" - string
    Price - "Text" - string

In this object, Address is a list of all addresses, notes is just another field of the object and items has a child named item and it contains Lists inside, just like address.

I am getting the value of notes by this code:

foreach (var item in (object as IEnumerable).Cast<object>().ToList())
                    {
    String substring = item.GetType().GetProperty("Notes").GetValue(item).ToString();
}

This is the return I get from the method when it passes to List:System.Collections.Generic.List1[Address]

But I can't access the lists, I need to separate this object in X Lists, like this:

Object2
 Adress
  [0]
  [1]

Object3
 Items
  Item
   [0]
   [1]

And then I can get the values using the method above, any suggestions? I already tried so many ways...

Lucio Zenir
  • 365
  • 2
  • 8
  • 18
  • 4
    It would be awesome if you could create a [mcve]. – mjwills Dec 12 '17 at 13:19
  • I need to get the values from the lists inside of the object, i can't get that, i'm just getting the values from notes (that is not a List) – Lucio Zenir Dec 12 '17 at 13:23
  • My `foreach` just return to me the notes values, when it passes the adress, i return : System.Collections.Generic.List1[Address] – Lucio Zenir Dec 12 '17 at 13:25
  • I think at least you just need to give a source of your Object to us, so we may help you. – Sergei Iashin Dec 12 '17 at 13:27
  • My object is created in another part of the program and it has that structure that i write on the question, how can i explain better? (i'm new at stack) – Lucio Zenir Dec 12 '17 at 13:29
  • @LucioZenir Take a look at the link that mjwills provided. Then ask yourself, "Is my question minimal?", "Is my question complete?", "Is my question Verifiable?". Look at your question as is and see if you can reproduce what is happening based solely on what you put in the question. I can tell you that at the very least you haven't shown us the types of any of the objects/fields/properties in your question. The question should be reproducible by anyone who reads it. Try to make some edits following the guidelines from the link that mjwills provided. – Sudsy1002 Dec 12 '17 at 13:43

1 Answers1

2

Just cast the value of Adress property to List<Object>, like this

foreach (var item in (object as IEnumerable).Cast<object>().ToList())
{
    List<Object> address = item.GetType().GetProperty("Adress").GetValue(item) as List<Object>;
    foreach(var ad in address)
    {
        //do what you want here
    }
}
aperezfals
  • 1,341
  • 1
  • 10
  • 26
  • List address = item.GetType().GetProperty("Address").GetValue(item) as List; this get's null. any clue why? – Lucio Zenir Dec 12 '17 at 13:57
  • That is because Address property is not a list, inspect it, like this `item.GetType().GetProperty("Address").GetValue(item).GetType()`. Maybe is a IEnumerable and not a List. You must inspect the property. – aperezfals Dec 12 '17 at 14:01
  • - item.GetType().GetProperty("Address").GetValue(item).GetType‌​() {Name = "List`1" FullName = "System.Collections.Generic.List`1[[Address, Address, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]"} System.Type {System.RuntimeType} it appears to be a list – Lucio Zenir Dec 12 '17 at 14:51
  • 1
    Cast it then to List
    like this: `List
    address = item.GetType().GetProperty("Address").GetValue(item) as List
    ;`
    – aperezfals Dec 12 '17 at 15:09
  • It still return null when i cast it to a List, maybe i'm doing something wrong – Lucio Zenir Dec 12 '17 at 15:36
  • i just got it, i needed to use 2 casts so i can get the values from the lists, thanks for the tips – Lucio Zenir Dec 12 '17 at 16:52