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 681959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:32:29+00:00 2026-05-14T01:32:29+00:00

My current LINQ query and example XML are below. What I’d like to do

  • 0

My current LINQ query and example XML are below. What I’d like to do is select the primary email address from the email-addresses element into the User.Email property.

  • The type element under the
    email-address element is set to primary when this is true.
  • There may
    be more than one element under the
    email-addresses but only one will be marked primary.

What is the simplest approach to take here?

Current Linq Query (User.Email is currently empty):

var users = from response in xdoc.Descendants("response")
            where response.Element("id") != null
            select new User
                       {
                           Id = (string)response.Element("id"),
                           Name = (string)response.Element("full-name"),
                           Email = (string)response.Element("email-addresses"),
                           JobTitle = (string)response.Element("job-title"),
                           NetworkId = (string)response.Element("network-id"),
                           Type = (string)response.Element("type")
                       };

Example XML:

<?xml version="1.0" encoding="UTF-8"?>
<response>
  <response>
    <contact>
      <phone-numbers/>
      <im>
        <provider></provider>
        <username></username>
      </im>
      <email-addresses>
        <email-address>
          <type>primary</type>
          <address>alice@domain.com</address>
        </email-address>
      </email-addresses>
    </contact>
    <job-title>Account Manager</job-title>
    <type>user</type>
    <expertise nil="true"></expertise>
    <summary nil="true"></summary>
    <kids-names nil="true"></kids-names>
    <location nil="true"></location>
    <guid nil="true"></guid>
    <timezone>Eastern Time (US &amp; Canada)</timezone>
    <network-name>Domain</network-name>
    <full-name>Alice</full-name>
    <network-id>79629</network-id>
    <stats>
      <followers>2</followers>
      <updates>4</updates>
      <following>3</following>
    </stats>
    <mugshot-url>
      https://assets3.yammer.com/images/no_photo_small.gif</mugshot-url>
      <previous-companies/>
      <birth-date></birth-date>
      <name>alice</name>
      <web-url>https://www.yammer.com/domain.com/users/alice</web-url>
      <interests nil="true"></interests>
      <state>active</state>
      <external-urls/>
      <url>https://www.yammer.com/api/v1/users/1089943</url>
      <network-domains>
        <network-domain>domain.com</network-domain>
      </network-domains>
      <id>1089943</id>
      <schools/>
      <hire-date nil="true"></hire-date>
      <significant-other nil="true"></significant-other>
    </response>
  <response>
    <contact>
      <phone-numbers/>
      <im>
        <provider></provider>
        <username></username>
      </im>
      <email-addresses>
        <email-address>
          <type>primary</type>
          <address>bill@domain.com</address>
        </email-address>
      </email-addresses>
    </contact>
    <job-title>Office Manager</job-title>
    <type>user</type>
    <expertise nil="true"></expertise>
    <summary nil="true"></summary>
    <kids-names nil="true"></kids-names>
    <location nil="true"></location>
    <guid nil="true"></guid>
    <timezone>Eastern Time (US &amp; Canada)</timezone>
    <network-name>Domain</network-name>
    <full-name>Bill</full-name>
    <network-id>79629</network-id>
    <stats>
      <followers>3</followers>
      <updates>1</updates>
      <following>1</following>
    </stats>
    <mugshot-url>
      https://assets3.yammer.com/images/no_photo_small.gif</mugshot-url>
      <previous-companies/>
      <birth-date></birth-date>
      <name>bill</name>
      <web-url>https://www.yammer.com/domain.com/users/bill</web-url>
      <interests nil="true"></interests>
      <state>active</state>
      <external-urls/>
      <url>https://www.yammer.com/api/v1/users/1089920</url>
      <network-domains>
        <network-domain>domain.com</network-domain>
      </network-domains>
      <id>1089920</id>
      <schools/>
      <hire-date nil="true"></hire-date>
      <significant-other nil="true"></significant-other>
    </response>
</response>
  • 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-14T01:32:30+00:00Added an answer on May 14, 2026 at 1:32 am

    Using Lambda Expression:

    var users = xdoc.Root.Elements( "response" )
        .Where( x => !string.IsNullOrEmpty( x.Element( "id" ).Value ) )
        .Select( x => new User
                  {
                    Id = x.Element( "id" ).Value,
                    Name = x.Element( "full-name" ).Value,
                    Email = x.Descendants( "email-address" )
                                .Where( y => y.Element( "type" ).Value == "primary" )
                                .First().Element( "address" ).Value,
                    JobTitle = x.Element( "job-title" ).Value,
                    NetworkId = x.Element( "network-id" ).Value,
                    Type = x.Element( "type" ).Value,
                  } );
    

    The Query Expression isn’t much different:

    var users = from response in xdoc.Descendants( "response" )
                where response.Element( "id" ) != null
                select new User
                {
                    Id = response.Element( "id" ).Value,
                    Name = response.Element( "full-name" ).Value,
                    Email = response.Descendants( "email-address" )
                                .Where( x => x.Element( "type" ).Value == "primary" )
                                .First().Element( "address" ).Value,
                    JobTitle = response.Element( "job-title" ).Value,
                    NetworkId = response.Element( "network-id" ).Value,
                    Type = response.Element( "type" ).Value
                };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was looking at a co-workers Linq query, shown below (the query executes correctly):
A followup to this previous question: Current code: var query = from b in
I have a fairly standard inheritance situation in my current LINQ-to-SQL project. I have
My current project is in Rails. Coming from a Symfony (PHP) and Django (Python)
I have this query: var iterator = criteria.binaryAssetBranchNodeIds.GetEnumerator(); iterator.MoveNext(); var binaryAssetStructures = from bas
I'm trying to dynamically create an object of a certain type in a LINQ-to-XML
My LINQ Query is only returning the first result (Class). Here is the code
i have a linq query that returns Articles ordered by the number of tags
Basically I want to write a linq query to order the number of days
Inside a compiled LINQ query as a condition part, I was using Datetime.Now .

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.