XDocument.Descendants("somethinghere").Count() method does not exist
I am trying to do a simple count of some descendants using LINQ to XML and the "Count()" method does not exist for me?
Example:
using System.Xml.Linq;
XDocument doc = XDocument.Load( "somexmlfile" );
int count = doc.Descendants("somethinghere").Count();
The ab开发者_JAVA技巧ove won't compile, because it doesn't recognize the Count()
method.
Do you have using System.Linq;
at the top of the file?
This is because Descendants(XName name)
returns an IEnumerable<XElement>
.
By design, IEnumerable lazy loads the data and so counting items in the collection would cause enumeration to occur.
I would probably convert this to an IList
which has a Count
property you can use.
精彩评论