[英]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 the properties.
但我需要到达 object 级别才能调用属性。
so here is the structure of my list when I go to thought the debugger to give you an idea how deep it is.所以这是我的列表的结构,当我 go 认为调试器让你知道它有多深时。
Name Value
ItemList count = 40
项目列表计数 = 40
-> [0] count = 1-> [0] 计数 = 1
--->[0] {namespace.class}--->[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.现在列表 SyncItems 正在添加到 ItemsList 以及其他项目中。
List<Object> ItemsList = new List<object>();
ItemsList.Add(SYNCItems);
So it's like a multi array list.所以它就像一个多数组列表。
Does this help?这有帮助吗?
(Edited in response to the latest information) (根据最新信息编辑)
You're using List<object>
to store what should be a List<List<Component>>
.您正在使用
List<object>
来存储应该是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.组件列表可以放置在 object 列表中,但它将被存储为 object 并且您将无法在不回滚的情况下将其作为列表访问。
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:) (如果您使用的是 C# 2.0,请指定类型名称而不是
var
,如下所示:)
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.然后,您应该能够通过
ItemList[0][0].Property
或通过 foreach 迭代等访问内部列表。
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>
.使用
List<Component>
而不是List<object>
。
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.当您说您的列表包含对象列表时,您是指列表中的列表还是仅包含超过 1 个 object 的列表。
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)我认为您想访问 ElementAt(int index) 的列表元素
Ah, I see your issue now.啊,我现在看到你的问题了。 Here you go.
这里是 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.为了完整起见,这里是 SomeClass。 :)
:)
public class SomeClass
{
public string SomeProperty { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.