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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:36:16+00:00 2026-06-13T04:36:16+00:00

In VB, the following is a valid object initializer in which one member intializer

  • 0

In VB, the following is a valid object initializer in which one member intializer references the value of another member that has previously been initialized.

new MyObject() with {.Property1="x", .Property2 = .Property1 + "y"}

If I try to do the same in C# using

new MyObject() {Property1 = "x", Property2 = Property1 + "y"}

I get the error

The name ‘Property1’ does not exist in the current context.

It is a little surprising since there is a fair amount of parity between the two languages but perhaps this is one of those few differences.

Is there a way to do this in C#? For those wondering what the specific use case may be, I am constructing a composite object structure using a fairly complex LINQ query.

IEnumerable<Question> query =
(from q in dtQuestions.AsEnumerable()
 join a in dtAnswers.AsEnumerable() on q.Field<int>("question_type_id") equals a.Field<int>("question_type_id") into Group
 select new Question()
 {
     Id = q.Field<int>("question_type_id"),
     SequenceNumber = q.Field<int>("sequence_no"),
     IsChild = q.Field<bool>("isChildQuestion"),
     EktronContentKey = q.Field<string>("ektron_content_key"),
     Text = q.Field<string>("description"),
     QuestionKindId = q.Field<int>("question_kind_type_id"),
     Answers = (from a2 in Group
                select new Answer()
                {
                    Id = a2.Field<int>("answer_type_id"),
                    SequenceNumber = a2.Field<int>("sequence_no"),
                    EktronContentKey = a2.Field<string>("ektron_content_key"),
                    Text = a2.Field<string>("description"),
                    IsSelected = a2.Field<bool>("isSelected"),
                    ImageKey = q.Field<int>("question_type_id") == 2 ? "" : (Id % 2 == 0 ? "heating-gas-modern.png" : "heating-gas-condensing.png"),
                    ChildQuestionIds =
                          (from r in dtAnswerChildQuestions.AsEnumerable()
                           where r.Field<int>("answer_type_id") == Id
                           select r.Field<int>("question_type_id")).ToArray()
                }).ToArray(),
     SelectedAnswerId = QuestionKindId == 1 ?
                            (from Answer a3 in Answers
                             where a3.IsSelected == true
                             select a3.Id).SingleOrDefault() :
                            0,
     SelectedAnswerIds = QuestionKindId == 2 ?
                           (from Answer a4 in Answers
                            where a4.IsSelected == true
                            select a4.id).ToArray() :
                            new int() { }
 }
);

The real problem to address in this is the reference to the Answers property in the LINQ expression used to assign values to SelectedAnswerId and SelectedAnswerIds. I may have to factor those two expressions out into their own standalone assignments.

  • 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-13T04:36:17+00:00Added an answer on June 13, 2026 at 4:36 am

    I wound up having to resolve this by using a secondary for loop. The code was a bit too complicated to do with LET statements.

        IEnumerable<Question> query =
            (from q in dtQuestions.AsEnumerable()
             join a in dtAnswers.AsEnumerable() on q.Field<int>("question_type_id") equals a.Field<int>("question_type_id") into Group
             select new Question()
             {
                 Id = q.Field<int>("question_type_id"),
                 SequenceNumber = q.Field<int>("sequence_no"),
                 IsChild = q.Field<bool>("isChildQuestion"),
                 EktronContentKey = q.Field<string>("ektron_content_key"),
                 Text = q.Field<string>("description"),
                 QuestionKindId = q.Field<int>("question_kind_type_id"),
                 Answers = new AnswerCollection((from a2 in Group
                                                 select new Answer()
                                                 {
                                                     Id = a2.Field<int>("answer_type_id"),
                                                     SequenceNumber = a2.Field<int>("sequence_no"),
                                                     EktronContentKey = a2.Field<string>("ektron_content_key"),
                                                     Text = a2.Field<string>("description"),
                                                     IsSelected = a2.Field<bool>("isSelected"),
                                                     ImageFileId = a2.Field<int?>("file_id"),
                                                     ChildQuestionIds =
                                                           new Collection<int>((from r in dtAnswerChildQuestions.AsEnumerable()
                                                                                where r.Field<int>("answer_type_id") == a2.Field<int>("answer_type_id")
                                                                                select r.Field<int>("question_type_id")).ToList())
                                                 }))
             }
          );
        foreach (var question in query)
        {
            question.SelectedAnswerId = question.QuestionKindId == 1 ?
                                        (from Answer a3 in question.Answers
                                         where a3.IsSelected == true
                                         select a3.Id).SingleOrDefault() :
                                        0;
            question.SelectedAnswerIds = question.QuestionKindId == 2 ?
                                         new Collection<int>((from Answer a4 in question.Answers
                                                              where a4.IsSelected == true
                                                              select a4.Id).ToList()) :
                                         new Collection<int>();
            this.Add(question);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

which of the following are valid object constructors? 1) var m = function(){} 2)
I'm using the following code to check if a valid date has been typed
I've been using my own object cache which works more or less: get objet
Is the following valid in Java: public Vector <Object> objVector = new Vector <Object>(50);
Suppose that I have the following python base class: class BaseClass(object): def a(): This
In COM how does one verify that a pointer to a COM object still
I have following nested objects. I am using @Valid for validation in my controller.
Apparently the following is valid in c# 4.0 regardless of the type of the
how do i make the following a valid date? class Program { static void
I would like to know if the following code are valid. The original intension

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.