与LinQ分开的关键价值

时间:2013-12-11 15:19:22

标签: c# linq json.net

我需要来自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; }
}

3 个答案:

答案 0 :(得分:2)

SharedToDomains.Select(x => x.Value).ToList().ForEach(x => Console.WriteLine(x));

答案 1 :(得分:1)

快捷方式:

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

答案 2 :(得分:1)

循环遍历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);