Enumerable.Where() not possible on IEnumerable?

I have an XmlNodeList and want to call the .Where() to get a filtered Node-List. But this doesn't work. Now I'm wondering, why sometimes the .Where() works and sometimes not. How could I get this code running with the .Where() ?

XmlDocument doc = new XmlDocument();
doc.LoadXml(Xml);
var oTestNodes = doc.DocumentElement.ChildNodes.Where((item) => item.Name = "TestNode");

To show what I mean, this is the query expression that would do the same:

var oTestNodes = from XmlNode oNode in doc.DocumentElement.ChildNodes
                 where oNode.Name = "TestNode"
                 select oNode;
Jon Skeet
people
quotationmark

Most LINQ to Objects methods work on the generic IEnumerable<T> type, rather than IEnumerable. Unfortunately, XmlNodeList only implements IEnumerable.

It sounds like you're just looking for Cast, which is what the query expression with the explicit range variable type uses (and hence why the query expressions compile):

XmlDocument doc = new XmlDocument();
doc.LoadXml(Xml);
var oTestNodes = doc.DocumentElement.ChildNodes
                    .Cast<XmlNode>()
                    .Where(item => item.Name = 'TestNode');

I'd personally recommend using LINQ to XML instead of XmlDocument if you possibly can though - it's a much nicer API.

people

See more on this question at Stackoverflow