List <Dictionary> foreach获得价值

vug*_*uze -1 c# asp.net dictionary

List<Dictionary<string, object>> data = new List<Dictionary<string, object>>();
Dictionary<string, Object> objs = new Dictionary<string, object>();
for (int i = 1; i <= onlineolanlar.Count; i++)
{
    objs.Add(i.ToString(), new userLevel() { UserID = userID.ToString(), Presence = UserLevelBul.ToString() });
}
foreach (var item5 in objs.Values)
{
    //How can I access item5 properties here??
}
Run Code Online (Sandbox Code Playgroud)

大家好,

我如何达到该foreach循环的值?

item5应该具有属性 useriduserlevelbul但我无法到达它们。

Ren*_*ogt 7

您将字典声明objsDictionary<string, object>。但是实际上,您只想添加(和以后使用)type条目userLevel

因此,只需使用正确的类型声明字典:

Dictionary<string, userLevel> objs = new Dictionary<string, userLevel>();
Run Code Online (Sandbox Code Playgroud)

然后,您可以直接使用它:

foreach (var item5 in objs.Values)
{
     Console.WriteLine(item5.userID)
     Console.WriteLine(item5.Presence)
}
Run Code Online (Sandbox Code Playgroud)

因为现在中的元素objs.Values以及item5类型都是userLevel