Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9216279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:23:03+00:00 2026-06-18T02:23:03+00:00

I am using below query to return all the child elements and then based

  • 0

I am using below query to return all the child elements and then based on child node i want to fetch 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 am having 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 want my value of 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 i can handle “Enumeration yielded no results” as count 0 in my return list since it’s giving count>0 if this error occurs.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:23:04+00:00Added an answer on June 18, 2026 at 2:23 am

    Result of query has type IEnumerable<List<IEnumerable<XElement>>>. But you are trying to assign it to IEnumerable<XElement>. Why? Because for each A element you are selecting list of A1 child elements. That gives you collection of lists.

    You can use explicit result type to solve this issue. I.e. instead of IEnumerable<XElement> lv1s use var lvls. Or use actual lvls type: IEnumerable<List<IEnumerable<XElement>>> lvls.

    Or if you want to get IEnumerable<XElement> then 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 your query 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 following elements:

      <A11>A1</A11>
      <A22>A2</A22>
      <A33>A3</A33>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Sql-Server, and I have the below query which returns all columns that
I am using the below query to return the names of artists which appear
I'm using below query to count the daily result, but now I'm make it
Using a query like below you can fetch rows in which the color of
I'm using the below query to return results from a table using Full-Text-Search. In
How would I return all category id's related to the result of a query?
I'm using below mentioned query with about 400,000 records in each table its taking
We have the query below. Using a LEFT OUTER join takes 9 seconds to
I am using SQL Query and below are the tables. Organization OrgID Name RAOGID
I am using SQL Query and below are the tables. Organization OrgID Name RAOGID

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.