gpt4 book ai didi

c# - 返回按属性匹配的对象列表

转载 作者:行者123 更新时间:2023-11-30 14:52:11 24 4
gpt4 key购买 nike

我有一个解析 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出:

Cannot implicitly convert type 'Classes.XMLprofile.Profile' to 'System.Collections.Generic.List<Classes.XMLprofile.Profile>'

最佳答案

您希望返回在 CurrentId 中具有正确 ID 的结果,在代码中,由于返回值是 Profile 对象而方法签名是 Profile 对象列表,因此出现编译器错误,因此:

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

关于c# - 返回按属性匹配的对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32366783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com