gpt4 book ai didi

c# - 如何遍历 List

转载 作者:太空宇宙 更新时间:2023-11-03 15:50:38 25 4
gpt4 key购买 nike

我有一个包含动态列表的类,我设法像这样访问它。

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

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”现在包含一个对象列表,但是不知道如何将这些值获取到字典或列表中。请注意,我还需要调整键名。

我尝试做的事情:

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

foreach 语句不能对“System.Collections.IEnumerable”类型的变量进行操作,因为“System.Collections.IEnumerable”不包含“GetEnumerator”的公共(public)定义

动态对象也没有给我访问键和/或值的选项。

非常欢迎任何帮助我如何遍历它的方法。

当我把它放在运行时时,“i”说它是一个 System.Collections.Generic.Dictionary 但是也可以访问它。

解决方法:

几乎没有机会解决所提供的问题,但这对我有用:

        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;
}
}
}

最佳答案

    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();
}

关于c# - 如何遍历 List<dynamic>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26013232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com