C# 检索按属性匹配的对象列表
我有一个解析C# 检索按属性匹配的对象列表,c#,list,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") selec
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(字符串文档)
{
列表结果=新列表();
doc=XDocument.Load(文档);
资源=(从文档子体(“级别”)中的n开始)
选择新配置文件()
{
CurrentID=int.Parse(n.Attribute(“CurrentID”).Value),
位置=(从n.Element(“ID”)元素(“ID”)中的l开始)
选择新位置()
{
id=(int)(l.属性(“id”),
x=(单个)l.属性(“x”),
y=(单个)l.属性(“y”),
z=(单个)l.属性(“z”)
}).ToList(),
银行=(从单据子代中的l(“银行”)
选择新银行家()
{
BankID=(int)(l.属性(“id”),
BankX=(单个)(l.属性(“x”)),
BankY=(单个)(l.属性(“y”)),
BankZ=(单个)(l.属性(“z”))
}).ToList(),
供应商=(从文件子体(“供应商”)中的l开始)
选择新供应商()
{
VendorID=(int)(l.属性(“id”),
VendorX=(单个)(l.属性(“x”)),
卖方=(单一)(l.属性(“y”)),
VendorZ=(单个)(l.属性(“z”))
})托利斯先生()
}).ToList();
var ProperID=resourcts.Where(s=>s.CurrentID c.CurrentID>d.CurrentID?c:d);
return ProperID;//错误:这里我要返回列表ProperID
}
我想解析XML文件,然后使用特定的CurrentID
从解析列表中获取节点。
我想返回ProperID
列表,但编译器错误
带:
无法将类型'Classes.XMLprofile.Profile'
隐式转换为'System.Collections.Generic.List'
您希望返回在CurrentId中具有正确id的结果, 在代码中,由于返回值是概要文件对象,而方法签名是概要文件对象列表,所以出现编译器错误:
return Resoults.Where(p=>p.CurrentID ==ProperID.CurrentID).ToList();
1.
resourcts
2的拼写。更新行'var ProperID=resourcts.Where(s=>s.CurrentID c.CurrentID>d.CurrentID?c:d).ToList();`并尝试得到与我发布的错误相同的错误。@Tagyoureit,不可能出现相同的错误,请解释更多错误。很抱歉,您的答案绝对正确!谢谢,我想知道你的票数!反对票()和赞成票(这个答案)!