ToArray、ToList、ToDictionary、ToLookup、OfType

 
          ToDictionary可以传两个lambada表达式,第一个是Key,第二个就是Value。
   //加载XML文件
    XDocument xdoc = XDocument.Load(fileName);
    XElement root = xdoc.Element("message");

  //读取XML内容
   messages = root.Elements("add")
    .ToDictionary(
        key => key.Attribute("key").Value,
        value => value.Attribute("value").Value
    );
 
#endregion

显示结果
ToArray、ToList、ToDictionary、ToLookup、OfType
 
ToDictionary() 不会处理重复键,该用 ToLookup()来处理重复键 来解决
 
var data = new[]{
            Tuple.Create("001", "James"),
            Tuple.Create("002", "Linda"),
            Tuple.Create("003", "Frank"),
            Tuple.Create("004", "Jack"),
            Tuple.Create("002", "Rose"),
            Tuple.Create("001", "Lynn"),
            Tuple.Create("008", "Luke")
        };

var dict = data.ToLookup(t => t.Item1, t => t.Item2)
            .ToDictionary(t => t.Key, t => t.First());
 
 
          ToDictionary可以传两个lambada表达式,第一个是Key,第二个就是Value。
   //加载XML文件
    XDocument xdoc = XDocument.Load(fileName);
    XElement root = xdoc.Element("message");

  //读取XML内容
   messages = root.Elements("add")
    .ToDictionary(
        key => key.Attribute("key").Value,
        value => value.Attribute("value").Value
    );
 
#endregion

显示结果
ToArray、ToList、ToDictionary、ToLookup、OfType