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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:48:08+00:00 2026-05-18T05:48:08+00:00

Am I thinking about this incorrectly?? Or missing something completely obvious? I can best

  • 0

Am I thinking about this incorrectly?? Or missing something completely obvious? I can best show by example. This code is how I initialize a new a new object now. It seems redundant.

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }

    public Person(int Id)
    {
        XDocument personXml = XDocument.Load("person.xml");
        var person = (from p in personXml.Descendants("Person")
                      where (int)p.Attribute("id") == Id
                      select new
                      {
                          FirstName = (string)p.Element("FirstName"),
                          LastName = (string)p.Element("LastName"),
                          Age = (int)p.Element("Age")
                      }).SingleOrDefault();

        //with the "select new" in query I have to set the properties manually
        FirstName = person.FirstName;
        LastName = person.LastName;
        Age = person.Age;
    }
}

And this is what I’m trying to do but can’t get it to work:

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }

    public Person(int Id)
    {
        XDocument personXml = XDocument.Load("person.xml");
        (from p in personXml.Descendants("Person")
         where (int)p.Attribute("id") == Id
         select
         {
             FirstName = (string)p.Element("FirstName"),
             LastName = (string)p.Element("LastName"),
             Age = (int)p.Element("Age")
         }).SingleOrDefault();
    }
}

I feel like there should be a way to accomplish what I am trying to do. I must be missing some syntactic or fundemetal understanding of why it doesn’t. Or maybe I’m simply crazy for thinking it should be done this way.

  • 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-18T05:48:08+00:00Added an answer on May 18, 2026 at 5:48 am

    This is not the purpose of LINQ. what you have there is a query projection which might have hundreds of rows coming from it. It wouldn’t make sense to set values external to the query there because only the last would stay.

    However, you could do something like this:

    public static Person CreatePerson(int Id)
    {
        return (from p in XDocument.Load("person.xml").Descendants("Person")
                where (int)p.Attribute("id") == Id
                select new Person
                       {
                           FirstName = (string)p.Element("FirstName"),
                           LastName = (string)p.Element("LastName"),
                           Age = (int)p.Element("Age")
                       }).SingleOrDefault();
    }
    

    Further important note:
    In general it is a bad idea to be doing I/O and other expensive (and possibly failure prone) work within constructors. You can often avoid problems by keeping constructors short and loading data in factory methods like this one.

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

Sidebar

Related Questions

Can I specify interfaces when I declare a member? After thinking about this question
I am thinking about something like this: public static <T extends Comparable<T>> T minOf(T...ts){
I'm thinking about making a networked game. I'm a little new to this, and
While thinking about this question and conversing with the participants, the idea came up
I've been thinking about this object oriented design question for a while now and
Maybe I'm just thinking about this too hard, but I'm having a problem figuring
I've been thinking about this problem for a while and have yet to come
A discussion about Singletons in PHP has me thinking about this issue more and
I'm thinking about how to do this, but I have several different shapes of
This recent question about sorting randomly using C# got me thinking about the way

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.