>" to "System.Collections.Generic.IEnumerable". Explicit co...">

It is not possible to implicitly convert the type "System.Collections.Generic.IEnumerable >>" to "System.Collections.Generic.IEnumerable". Explicit conversion exists

I use the below query to return all children, and then based on the child node. I want to get the result.

XElement rootElement = XElement.Load(@"E:\Samples\TestConsole\TestConsoleApp\TestConsoleApp\XMLFile1.xml"); IEnumerable<XElement> lv1s = from lv1 in rootElement.Descendants("A") where lv1.Attribute("Code").Value.Equals("A001") && lv1.Attribute("Lable").Value.Equals("A001") select (from ltd in lv1.Descendants("A1") where ltd.Attribute("Code").Value.Equals("001") select ltd.Elements()).ToList(); 

But still, I have the following error.

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Collections.Generic.List<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?)

Please let me know.

I need my value lv1s ["A11"], lv1s ["A22"]

below is my xml

  <?xml version="1.0" encoding="utf-8" ?> <Document> <A Code="A001" Lable="A001"> <A1 Code="001"> <A11>A1</A11> <A22>A2</A22> <A33>A3</A33> </A1> </A> <A Code="A002" Label="A002"> <A1 Code="002"> <A44>A44</A44> <A55>A55</A55> </A1> </A> </Document> 

Please let me know.

Also, how can I handle the "enumeration did not give any results" as count 0 in my return list, since it gives count> 0 if this error occurs.

+4
source share
2 answers

The query result is of type IEnumerable<List<IEnumerable<XElement>>> . But you are trying to assign it to IEnumerable<XElement> . What for? Because for each element A you select a list of child elements A1 . This gives you a list of listings.

You can use an explicit result type to solve this problem. That is, instead of IEnumerable<XElement> lv1s use var lvls . Or use the actual lvls type: IEnumerable<List<IEnumerable<XElement>>> lvls .

Or, if you want to get an IEnumerable<XElement> , use SelectMany :

 IEnumerable<XElement> lv1s = rootElement.Descendants("A") .Where(lv1 => (string)lv1.Attribute("Code") == "A001" && (string)lv1.Attribute("Lable") == "A001") .SelectMany(lv1 => lv1.Descendants("A1") .Where(ltd => (string)ltd.Attribute("Code") == "001") .Select(ltd => ltd.Elements()); 

Or rewrite the request this way:

  IEnumerable<XElement> lv1s = from lv1 in rootElement.Descendants("A") where (string)lv1.Attribute("Code") == "A001" && (string)lv1.Attribute("Lable") == "A001" from ltd in lv1.Descendants("A1") where (string)ltd.Attribute("Code") == "001" from e in ltd.Elements() select e; 

Returns the following elements:

  <A11>A1</A11> <A22>A2</A22> <A33>A3</A33> 
+3
source

Suggestion: instead of using .Value , enter as string . Also, if your variable is in any case equal to IEnumerable<XElement> rather than List<XElement> , why do you need a ToList ?

 XElement rootElement = XElement.Load(@"E:\Samples\TestConsole\TestConsoleApp\TestConsoleApp\XMLFile1.xml"); IEnumerable<XElement> lv1s = from lv1 in rootElement.Descendants("A") where (string)lv1.Attribute("Code") == "A001" && (string)lv1.Attribute("Lable") == "A001" select ( from ltd in lv1.Descendants("A1") where (string)ltd.Attribute("Code") == "001" select ltd.Elements() ); 

You have a LINQ subquery, and your query also returns an IEnumerable<XElement> . Try to remove the subquery and return one element as a result of the query (parentheses are added only for clarity):

 IEnumerable<XElement> lv1s = ( from lv1 in rootElement.Descendants("A") where (string)lv1.Attribute("Code") == "A001" && (string)lv1.Attribute("Lable") == "A001" from ltd in lv1.Descendants("A1") where (string)ltd.Attribute("Code") == "001" from e in ltd.Elements() select e ); 

Alternatively, if you are looking for only one level of depth, you can use Elements instead of Descendants :

 IEnumerable<XElement> lv1s = ( from lv1 in rootElement.Elements("A") where (string)lv1.Attribute("Code") == "A001" && (string)lv1.Attribute("Lable") == "A001" from ltd in lv1.Elements("A1") where (string)ltd.Attribute("Code") == "001" from e in ltd.Elements() select e ); 

And if all you need is the values โ€‹โ€‹of these elements - A1 , A2 and A3 - instead of XElement objects:

 IEnumerable<string> lv1s = ( from lv1 in rootElement.Elements("A") where (string)lv1.Attribute("Code") == "A001" && (string)lv1.Attribute("Lable") == "A001" from ltd in lv1.Elements("A1") where (string)ltd.Attribute("Code") == "001" from e in ltd.Elements() select (string)e ); 
+1
source

All Articles