在将XElement添加到列表中时,我不能像通常使用字符串或整型数据列表那样执行查找。请建议我必须在下面做什么更改才能使其作为myIndexCase1或myIndexCase2工作?
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml;
using System.Text;
XElement x1 = new XElement("groupA", new XAttribute("Name","red"));
XElement x2 = new XElement("groupA", new XAttribute("Name","blue"));
XElement x3 = new XElement("groupA", new XAttribute("Name", "green"));
XElement x4 = new XElement("groupB", new XAttribute("Name", "white"));
XElement x5 = new XElement("groupB", new XAttribute("Name", "black"));
List<XElement> myList = new List<XElement>();
myList.Add(x1);
myList.Add(x2);
myList.Add(x3);
myList.Add(x4);
myList.Add(x5);
//We know x2 belongs to index = 1 but this syntax doesn't work ..it complains can not convert XElement to Predicates
int myIndexCase1 = myList.FindIndex(x2);
//And if I try this too also doesn't work
int myIndexCase2 = myList.FindIndex(s => x1.XPathSelectElements("group[@Name='blue']");发布于 2021-02-19 18:34:07
FindIndex需要Predicate<XElement>,而不是XElement。
这是一个谓词,您可以使用它来查找要查找的元素:
int myIndexCase1 = myList.FindIndex(element => element.Name == "groupA" &&
element.Attribute("Name").Value == "blue");https://stackoverflow.com/questions/66275835
复制相似问题