如何使用LINQ to XML按属性查找XML元素?

Ale*_*gas 3 .net xml linq linq-to-xml

我正在学习LINQ to XML,并且需要找到具有特定属性的元素的存在.目前我正在使用:

XElement groupCollectionXml = XElement.Parse(groupCollection.Xml);
IEnumerable<XElement> groupFind =
    from vw in groupCollectionXml.Elements("Group")
    where (string) vw.Attribute("Name") == groupName
    select vw;

if (groupFind.Count() == 0)
    return false;
else
    return true;
Run Code Online (Sandbox Code Playgroud)

我知道有一种更简洁的方法,可能使用Any(),但我不知道如何重写查询以使用它.有没有人有一些好的建议?谢谢.

egl*_*ius 6

groupCollectionXml.Elements("Group").Any(
    vw=>(string)vw.Attribute("Name") == groupName
  );
Run Code Online (Sandbox Code Playgroud)