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

  • Home
  • SEARCH
  • 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 3751128
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:01:49+00:00 2026-05-19T09:01:49+00:00

Hi trying to write a simple linq query from a tutorial I read. But

  • 0

Hi trying to write a simple linq query from a tutorial I read. But i cannot seem to get it to work. I am trying to display both the address in the attached xml document, but can only display the first one. Can someone help me figure out why both aren’t being printed. Thank you very much

<?xml version="1.0" encoding="utf-8" ?>
<Emails>
  <Email group="FooBar">
    <Subject>Test subject</Subject>
    <Content>Test Content</Content>
    <EmailTo>
      <Address>foo@bar.com</Address>
      <Address>bar@foo.com</Address>
    </EmailTo>
  </Email>
</Emails> 




    Dim steve = (From email In emailList.Descendants("Email") _
                Where (email.Attribute("group").Value.Equals("FooBar")) _
                Select content = email.Element("EmailTo").Descendants("Address")).ToList()



    If Not steve Is Nothing Then
        For Each addr In steve
            Console.WriteLine(addr.Value)
        Next
        Console.ReadLine()
    End If
  • 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-05-19T09:01:50+00:00Added an answer on May 19, 2026 at 9:01 am

    Your current query returns a List<IEnumerable<XElement>>. That means you need two nested foreach loops: one to loop over the list, and another to loop over the content of the IEnumerable<XElement>.

    Instead, you could update your LINQ query to use the Enumerable.SelectMany method and get to the addresses directly. In query format a SelectMany is represented by using a second from clause to indicate a subquery. This would resemble the following:

    Dim query = (From email In emailList.Descendants("Email") _
                Where (email.Attribute("group").Value.Equals("FooBar")) _
                From addr In email.Element("EmailTo").Descendants("Address") _
                Select addr.Value).ToList()
    
    If query.Any() Then
        For Each addr In query
            Console.WriteLine(addr)
        Next
    End If
    

    Also, the ToList isn’t needed if you only want to iterate over the results and don’t intend to use the result as a list for other purposes.

    EDIT: to explain how this query works let’s break it down in 2 parts:

    First:

    From email In emailList.Descendants("Email") _
    Where (email.Attribute("group").Value.Equals("FooBar")) _
    

    This queries for all <Email> nodes and only matches the ones that have a group attribute value of “FooBar”.

    Second:

    From addr In email.Element("EmailTo").Descendants("Address") _
    Select addr.Value
    

    This is a subquery that continues where the first part (above) ended. It essentially is a way to further query the results of the original query. Here we query for all <Address> nodes and, finally, select their Value for the inner text of the nodes. The reason we need to do this is because Descendants("Address") returns a IEnumerable<XElement> containing all “Address” elements. We need to perform an additional query (or foreach) to iterate over those values and extract their values.

    Another way to illustrate this is by breaking it down in 2 queries:

    Dim query1 = From email In emailList.Descendants("Email") _
                 Where (email.Attribute("group").Value.Equals("FooBar"))
                 Select email.Element("EmailTo").Descendants("Address")
    Dim query2 = query1.SelectMany(Function(addr) addr.Select(Function(a) a.Value))
    

    Notice the use of SelectMany in query2. The Select in query2 is that additional effort to loop over the IEnumerable that I mentioned earlier. The original query is clearer than query1/query2, but I wrote them just to clarify the point.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There should be a simple Linq query for what I'm trying to accomplish, but
I am Fairly new to Linq and I am trying to write a simple
i am trying to write a linq query that doesn't fire a million child
I'm trying to write a LINQ query that that simply gets the count of
I'm trying to perform a simple Linq find first query on a typed DataTable,
im trying to write a simple gui form to display if any database connections
I am trying to write a simple stream, streaming everything from a mysql field.
I am trying to write a simple Ruby program that I will run from
I am trying to write simple Visual Studio Add-In for code generation. In my
I'm trying to write a simple game/utility to calculate poker odds. I know there's

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.