开发者

List of objects obtaining information

I am still having issues getting my data out of a list. My list contains a list of objects. (if that makes sense)

I think it's just a syntax issue. but I need to get to the object level to call 开发者_如何学Pythonthe properties.

so here is the structure of my list when I go to thought the debugger to give you an idea how deep it is. Name Value

ItemList count = 40

-> [0] count = 1

--->[0] {namespace.class}

----> Propties Values

Now at this point I can see the properties and the values to these properties.

What is the syntax to access these properties?

I will describe how my structure is setup. So here is my list of my "Component objects"

List<Component> SYNCItems = new List<Component>();

All properties are being assigned to the objects and added into this list.

Now list SyncItems is being added to ItemsList amoung other items.

List<Object> ItemsList = new List<object>();

ItemsList.Add(SYNCItems);

So it's like a multi array list.

Does this help?


Misread the question at first. Assuming you mean something like:

List<List<Person>>;

Here is an example:

public class Person
{
    public string Name {get;set;}
}

List<List<Person>> superHeroes = new List<List<Person>>();
List<Person> persons = new List<Person>();

Person p = new Person {Name = "Superman"};
Person p2 = new Person {Name = "Batman"};
Person p3 = new Person {Name = "Spiderman"};

persons.Add(p);
persons.Add(p2);
persons.Add(p3);

superHeroes.Add(persons);

string batmanName = superHeroes[0][0].Name; //returns "Superman"

When you say your list contains a list of objects, do you mean a list within a list or just a list that contains more than 1 object.


If you just want to read the data out, you could get the inner list.

var innerList = ItemList.SelectMany(i => ((List<Component>)i).ToList());

Then you can access the properties:

foreach(var prop in innerList)
      //do something with prop.myProperty

EDIT - Updated answer in reply to edited question. Use List<Component> instead of List<object>.


(Edited in response to the latest information)

You're using List<object> to store what should be a List<List<Component>>. The component list can be placed inside the object list, but it'll be stored as an object and you will no longer be able to access it as a list without casting back.

Change to the following:

List<Object> ItemsList = new List<object>();

to

var ItemsList = new List<List<Component>>(); // you can use type inference to save you some typing

(If you're using C# 2.0, specify type name instead of var as below:)

List<List<Component>> ItemsList = new List<List<Component>>();

You should then be able to access the inner list via ItemList[0][0].Property or by foreach iteration etc.


How about

  ItemList.ElementAt(0).ElementAt(0).Properties

Or something to that effect.

I think you want to access the list elements which is ElementAt(int index)


Ah, I see your issue now. Here you go. Try this:

        List<SomeClass> SYNCItems = new List<SomeClass>();

        var obj1 = new SomeClass() { SomeProperty = "test" };
        SYNCItems.Add(obj1);

        var obj2 = new SomeClass() { SomeProperty = "test" };
        SYNCItems.Add(obj2);

        List<Object> ItemsList = new List<object>();

        ItemsList.Add(SYNCItems);


        var list = (ItemsList[0]) as List<SomeClass>;

        Console.WriteLine(list[0].SomeProperty);
        Console.WriteLine(list[1].SomeProperty); 

And just for completeness, here is SomeClass. :)

public class SomeClass
{
     public string SomeProperty { get; set; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜