开发者

XDocument.Descendants("somethinghere").Count() method does not exist

开发者 https://www.devze.com 2023-03-10 05:03 出处:网络
I am trying to do a simple count of some descendants using LINQ to XML and the \"Count()\" method does not exist for me?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号