title: How to reference another property value in a C# member initializer?
body: <p>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.</p>
  
  <p><code>new MyObject() with {.Property1="x", .Property2 = .Property1 + "y"}</code></p>
  
  <p>If I try to do the same in <code>C#</code> using</p>
  
  <pre><code>new MyObject() {Property1 = "x", Property2 = Property1 + "y"}
  </code></pre>
  
  <p>I get the error</p>
  
  <blockquote>
    <p>The name 'Property1' does not exist in the current context.</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>Is there a way to do this in <code>C#</code>?  For those wondering what the specific use case may be, I am constructing a composite object structure using a fairly complex LINQ query.</p>
  
  <pre><code>IEnumerable&lt;Question&gt; query =
  (from q in dtQuestions.AsEnumerable()
   join a in dtAnswers.AsEnumerable() on q.Field&lt;int&gt;("question_type_id") equals a.Field&lt;int&gt;("question_type_id") into Group
   select new Question()
   {
       Id = q.Field&lt;int&gt;("question_type_id"),
       SequenceNumber = q.Field&lt;int&gt;("sequence_no"),
       IsChild = q.Field&lt;bool&gt;("isChildQuestion"),
       EktronContentKey = q.Field&lt;string&gt;("ektron_content_key"),
       Text = q.Field&lt;string&gt;("description"),
       QuestionKindId = q.Field&lt;int&gt;("question_kind_type_id"),
       Answers = (from a2 in Group
                  select new Answer()
                  {
                      Id = a2.Field&lt;int&gt;("answer_type_id"),
                      SequenceNumber = a2.Field&lt;int&gt;("sequence_no"),
                      EktronContentKey = a2.Field&lt;string&gt;("ektron_content_key"),
                      Text = a2.Field&lt;string&gt;("description"),
                      IsSelected = a2.Field&lt;bool&gt;("isSelected"),
                      ImageKey = q.Field&lt;int&gt;("question_type_id") == 2 ? "" : (Id % 2 == 0 ? "heating-gas-modern.png" : "heating-gas-condensing.png"),
                      ChildQuestionIds =
                            (from r in dtAnswerChildQuestions.AsEnumerable()
                             where r.Field&lt;int&gt;("answer_type_id") == Id
                             select r.Field&lt;int&gt;("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() { }
   }
  );
  </code></pre>
  
  <p>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.</p>