C# linq select 从 XElement 树中复制
C# linq select duplicate from an XElement tree
这是我的 XElement 树
XElement myShoppingList = new XElement("myShoppingList",
new XElement("Pasadena", new XAttribute("Vendor", "Tesla"),
new XElement("Car",
new XElement("model3",
new XElement("Black")))),
new XElement("LasVegas", new XAttribute("Vendor", "Tesla"),
new XElement("Car",
new XElement("modelY",
new XElement("White")))),
new XElement("Pasadena", new XAttribute("Vendor", "Apple"),
new XElement("Phone",
new XElement("model13",
new XElement("Black")))),
new XElement("Pasadena", new XAttribute("Vendor", "Apple"),
new XElement("Phone",
new XElement("model12",
new XElement("White")))));
我似乎无法解决这个问题,第一个问题 - 在搜索“Pasadena”时,我应该重复 3 次:
var query = myShoppingList.Elements().AsEnumerable()
.GroupBy(x => x.XPathSelectElement("Pasadena"))
.Where(g => g.Count() > 1)
.Select(y => new { Element = y.Key, Counter = y.Count() })
.ToList();
在拉斯维加斯搜索特斯拉时,我期待 1 次重复
var query = myShoppingList.Elements().AsEnumerable()
.GroupBy(x => x.XPathSelectElement("Pasadena[@Name='Tesla']"))
.Where(g => g.Count() > 1)
.Select(y => new { Element = y.Key, Counter = y.Count() })
.ToList();
最后我想搜索“White”,不管这个小部件是谁制作的,然后显示它的制作者。所以我期待 2 次重复:
var query = myShoppingList.Elements().AsEnumerable()
.GroupBy(x => x.XPathSelectElement("../../../White"))
.Where(g => g.Count() > 1)
.SelectMany(y => y})
.ToList();
我无法正常工作我做错了什么?
I can't seems to get this working, first question - When searching for
"Pasadena" I should be getting 3 repeats:
这是您要实现的目标吗? :
var query = myShoppingList.XPathSelectElements("Pasadena")
.GroupBy(x => x.Name)
.Select(y => new { Element = y.Key, Counter = y.Count() });
When searching for Tesla in LasVegas I'm expecting 1 repeat
你可以这样试试
var query = myShoppingList.XPathSelectElements("Pasadena")
.Where(x => x.Attributes().Any(x => x.Name == "Vendor" && x.Value == "Tesla"))
.GroupBy(x => x.Name)
.Select(y => new { Element = y.Key, Counter = y.Count() });
对于这个,我不确定这是否是您想要实现的目标
var query = myShoppingList.XPathSelectElements("//White")
.GroupBy(x => x.Name)
.Select(y => new { Element = y.Key, Counter = y.Count() });
这是我的 XElement 树
XElement myShoppingList = new XElement("myShoppingList",
new XElement("Pasadena", new XAttribute("Vendor", "Tesla"),
new XElement("Car",
new XElement("model3",
new XElement("Black")))),
new XElement("LasVegas", new XAttribute("Vendor", "Tesla"),
new XElement("Car",
new XElement("modelY",
new XElement("White")))),
new XElement("Pasadena", new XAttribute("Vendor", "Apple"),
new XElement("Phone",
new XElement("model13",
new XElement("Black")))),
new XElement("Pasadena", new XAttribute("Vendor", "Apple"),
new XElement("Phone",
new XElement("model12",
new XElement("White")))));
我似乎无法解决这个问题,第一个问题 - 在搜索“Pasadena”时,我应该重复 3 次:
var query = myShoppingList.Elements().AsEnumerable()
.GroupBy(x => x.XPathSelectElement("Pasadena"))
.Where(g => g.Count() > 1)
.Select(y => new { Element = y.Key, Counter = y.Count() })
.ToList();
在拉斯维加斯搜索特斯拉时,我期待 1 次重复
var query = myShoppingList.Elements().AsEnumerable()
.GroupBy(x => x.XPathSelectElement("Pasadena[@Name='Tesla']"))
.Where(g => g.Count() > 1)
.Select(y => new { Element = y.Key, Counter = y.Count() })
.ToList();
最后我想搜索“White”,不管这个小部件是谁制作的,然后显示它的制作者。所以我期待 2 次重复:
var query = myShoppingList.Elements().AsEnumerable()
.GroupBy(x => x.XPathSelectElement("../../../White"))
.Where(g => g.Count() > 1)
.SelectMany(y => y})
.ToList();
我无法正常工作我做错了什么?
I can't seems to get this working, first question - When searching for "Pasadena" I should be getting 3 repeats:
这是您要实现的目标吗? :
var query = myShoppingList.XPathSelectElements("Pasadena")
.GroupBy(x => x.Name)
.Select(y => new { Element = y.Key, Counter = y.Count() });
When searching for Tesla in LasVegas I'm expecting 1 repeat
你可以这样试试
var query = myShoppingList.XPathSelectElements("Pasadena")
.Where(x => x.Attributes().Any(x => x.Name == "Vendor" && x.Value == "Tesla"))
.GroupBy(x => x.Name)
.Select(y => new { Element = y.Key, Counter = y.Count() });
对于这个,我不确定这是否是您想要实现的目标
var query = myShoppingList.XPathSelectElements("//White")
.GroupBy(x => x.Name)
.Select(y => new { Element = y.Key, Counter = y.Count() });