Search code examples
c#linq-to-xmlxelement

Why is Select not Recognized as a Valid Method of XElement?


Based on the code I see here, my almost-identical code should work:

XDocument doc = XDocument.Parse(stringifiedXML);
var Platypi = doc.Descendants("Platypus").Select(delItem => new
{
    Name = delItem.Element("duckbillName").Value,
    Length = delItem.Element("length").Value,
    Weight = delItem.Element("weight").Value,
    Age = delItem.Element("age").Value,
}).ToList();

Yet instead it violently jerks the rug from under my lazyboy and spills me on the floor, mocking my discomfiture with, "'System.Collections.Generic.IEnumerable' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)"

The question asked may rightly be responded to affirmatively, but right-clicking "Select" does not afford a "Resolve" context menu item, so I don't know what, if anything, I might be missing.


Solution

  • Just import the System.Linq namespace with:

    using System.Linq;
    

    All LINQ extension methods defined in the Enumerable class which is under the System.Linq namespace.If you are using .NET Framework 4.5.1 version then you should be able to use LINQ methods after you add your reference.