Linq - Cannot implicitly convert type 'IEnumerable<IEnumerable<.XElement>>' to 'IEnumerable<.XElement>'

I'm receiving the following error :

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>>' to 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>'. An explicit conversion exists (are you missing a cast?) 

And i don't understand why. Here you have my code where I try to get the DataContainer element which the Name attribute's is the same as name.key :

 XDocument xml = XDocument.Load(name.Value);
 IEnumerable<XElement> columns = from d in xml.Descendants("DataContainer")
                         where (d.Attribute("Name").Value.Equals(name.Key))
                         select d.Descendants("ArrayOfColumn");

And here you have my XML file :

<?xml version="1.0" encoding="utf-8"?>
    <DataContainer xmlns:xsi="" xmlns:xsd="" Name="IRS vs E3M" Currency="EUR">
        <ArrayOfColumn Name="Maturite" isData="false">
            <Line Value="1Y" />
            <Line Value="2Y" />
            <Line Value="3Y" />
            <Line Value="4Y" />
            <Line Value="5Y" />
            <Line Value="6Y" />
            <Line Value="7Y" />
            <Line Value="8Y" />
            <Line Value="9Y" />
            <Line Value="10Y" />
            <Line Value="15Y" />
            <Line Value="20Y" />
            <Line Value="30Y" />
            <Line Value="40Y" />
            <Line Value="50Y" />
            <Line Value="60Y" />
        <ArrayOfColumn Name="Value" isData="true">
            <Line Value="EURAB3E1Y" />
            <Line Value="EURAB3E2Y" />
            <Line Value="EURAB3E3Y" />
            <Line Value="EURAB3E4Y" />
            <Line Value="EURAB3E5Y" />
            <Line Value="EURAB3E6Y" />
            <Line Value="EURAB3E7Y" />
            <Line Value="EURAB3E8Y" />
            <Line Value="EURAB3E9Y" />
            <Line Value="EURAB3E10Y" />
            <Line Value="EURAB3E15Y" />
            <Line Value="EURAB3E20Y" />
            <Line Value="EURAB3E30Y" />
            <Line Value="EURAB3E40Y" />
            <Line Value="EURAB3E50Y" />
            <Line Value="EURAB6E60Y" />

Someone can help me and explain why please ?


The problem is that your d is a collection of descendants, then you are filtering them and pulling the further descendants from any that pass through the filter.

Even if your filter only matches to one element, the system will still treat it as a collection. If you know that there will only be one result, then you can use the .Single() method on d before pulling the descendants., e.g.:

XDocument xml = XDocument.Load(name.Value); 
IEnumerable<XElement> columns = xml.Descendants("DataContainer") 
                                   .Where(d => d.Attribute("Name").Value.Equals(name.Key)) 
                                   .Select(d => d.Descendants("ArrayOfColumn")); 

I've changed it to method chaining as it flows better for this. Note also that the where expression could be put into the single method, however this is clearer to look at.

Need Your Help

Entity Framework Specification Pattern Implementation

c# design-patterns entity-framework

How-to implement Specification Pattern with Entity Framework ?

Windows batch file - Pick (up to) four random files in a folder

random batch-file for-loop variable-expansion delayedvariableexpansion

As the title says, I'm trying to pick up to four random files (wallpapers) from a folder, for further processing. The folder does not contain subfolders, just *.jpg's, *.bmp's and *.png's (it may c...