简体   繁体   中英

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"?>
<DataContainers>
    <DataContainer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 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>
        <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" />
        </ArrayOfColumn>
    </DataContainer>
</DataContainers>

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., eg:

XDocument xml = XDocument.Load(name.Value); 
IEnumerable<XElement> columns = xml.Descendants("DataContainer") 
                                   .Where(d => d.Attribute("Name").Value.Equals(name.Key)) 
                                   .Single()
                                   .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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM