代码之家  ›  专栏  ›  技术社区  ›  keithwarren7

我们能用linq把这句话说得更简洁些吗?

  •  2
  • keithwarren7  · 技术社区  · 15 年前

        XDocument reader = XDocument.Load("sl.config");
        var configValues = from s in reader.Descendants("add") select new { Key = s.Attribute("key").Value, s.Attribute("value").Value };
    
        Dictionary<string, string> Settings = new Dictionary<string, string>();
    
        foreach (var s in configValues)
        {
            Settings.Add(s.Key, s.Value);
        }
    
    2 回复  |  直到 15 年前
        1
  •  6
  •   Denis K    15 年前

    尝试 Enumerable.ToDictionary 扩展方法。

     XDocument reader = XDocument.Load("sl.config");
     var Settings = reader.Descendants("add")
       .ToDictionary(s => s.Attribute("key").Value, s => s.Attribute("value").Value);
    
        2
  •  2
  •   Arnis Lapsa    15 年前
    var = XDocument.Load("sl.config").Descendants("add").ToDictionary
        (x => x.Attribute("key"). Value, x => x.Attribute("value"). Value);