Multiple descendants types linq

2019-07-06 13:30发布

问题:

I sometimes do this:

XElement.Descendants("mynodename");

is there a way to do something like this"

XElement.Descendants("mynodename or myothernodename");

回答1:

Not in one method call - but you can use:

element.Descendants()
       .Where(x => x.Name.LocalName == "mynodename" 
                   || x.Name.LocalName == "myothernodename")


回答2:

Or,

XElement.Descendants("mynodename")
  .Union(XElement.Descendants("myothernodename"));

Which would sort them by type, then in order of appearance...