110,534
社区成员
发帖
与我相关
我的任务
分享
public class Sample
{
public string Name { get; set; }
public string Sup { get; set; }
}
List<Sample> list = new List<Sample>()
{
new Sample()
{
Name = "大兴",
Sup = "北京"
},
new Sample()
{
Name = "北京",
Sup = "中国"
},
new Sample()
{
Name = "中国",
Sup = null,
}
};
Sample result = list.Aggregate(list[0],(child,next) =>
{
if (string.IsNullOrEmpty(child.Sup))
{
return child;
}
else
{
return list.FirstOrDefault(n => n.Name == child.Sup);
}
});
var elements = xDom.Descendants("上级分类").Where(t => GetParent(t).Value=="中国");
private System.Xml.Linq.XElement GetParent(System.Xml.Linq.XElement n) {
while (n != null && n.Parent != null) {
n = n.Parent;
}
return n;
}