按属性重新匹配对象列表

时间:2015-09-03 04:07:17

标签: c# list

我有一种解析XML的方法:

public static List<Profile> Parse XML(string Document)
{
    List<Profile> Result = new List<Profile>();     
    doc = XDocument.Load(Document);

    Resoults = (from n in doc.Descendants("level")
               select new Profile()
               {
                   CurrentID = int.Parse(n.Attribute("CurrentID").Value),    
                   Location = (from l in n.Element("ID").Elements("ID")
                              select new Location()
                              {
                                   id = (int)(l.Attribute("id")),
                                   x = (Single)l.Attribute("x"),
                                   y = (Single)l.Attribute("y"),
                                   z = (Single)l.Attribute("z")
                              }).ToList(),    
                   Bank = (from l in doc.Descendants("Banker")
                              select new Banker()
                              {
                                   BankID = (int)(l.Attribute("id")),
                                   BankX = (Single)(l.Attribute("x")),
                                   BankY = (Single)(l.Attribute("y")),
                                   BankZ = (Single)(l.Attribute("z"))
                              }).ToList(),    
                   Vendor = (from l in doc.Descendants("Vendor")
                              select new Vendor()
                              {
                                   VendorID = (int)(l.Attribute("id")),
                                   VendorX = (Single)(l.Attribute("x")),
                                   VendorY = (Single)(l.Attribute("y")),
                                   VendorZ = (Single)(l.Attribute("z")) 
                              }).ToList()
              }).ToList();      
    var ProperID =  Resoults.Where(s => s.CurrentID <= 10).Aggregate((c, d) => c.CurrentID > d.CurrentID ? c : d);    
    return ProperID;  //error: Here i want to return list ProperID
}

我想解析XML文件,然后从特定CurrentID的解析列表中获取节点。 我想返回ProperID列表但编译器errores以及:

  

无法隐式将'Classes.XMLprofile.Profile'类型转换为'System.Collections.Generic.List<Classes.XMLprofile.Profile>'

1 个答案:

答案 0 :(得分:5)

您想要在CurrentId中返回具有正确ID的结果, 在代码中你得到了编译错误,因为返回值是一个Profile对象,而方法签名是一个Profile对象的List,所以:

return Resoults.Where(p=>p.CurrentID ==ProperID.CurrentID).ToList();