我需要SharedToDomains和SharedFromDomains中的键和值数据。我想打印这些值。
var LogResponse = DeserializeFromJson<AttributeContainer>(sLogResponse);
public class AttributeContainer
{
public Dictionary<string, int> MimeTypes { get; set; }
public Dictionary<string, Domain> SharedToDomains { get; set; }
public Dictionary<string, Domain> SharedFromDomains { get; set; }
}
SharedToDomains.Select(x => x.Value).ToList().ForEach(x => Console.WriteLine(x));
一种快速方法:
foreach( var s in SharedToDomains.Keys )
{
string key = s;
string val = SharedToDomains[s].ToString();
}
如果你真的想使用linq:
var outputList = from s in SharedToDomains select new { key=s.Key, value=s.Value };
循环遍历Dictionary的值集合,并将域打印到屏幕上。我已经把它放在一个方法中,这样你以后就可以重复使用:
private void PrintValues(Dictionary<string, Domain> dict)
{
foreach(KeyValuePair<string, Domain> kvp in dict)
{
Console.WriteLine(kvp.Key); //Key
Console.WriteLine(kvp.Value.ToString()); //Value
}
}
//Usage:
PrintValues(SharedToDomains);
PrintValues(SharedFromDomains);