title: Conditionally assign C# var: As elegant as it gets?
body: <p>I understand that the C# keyword <em>var</em> implies the type at compile time and therefore requires the declaration and assignment in the same place, making the following construct illegal:</p>
  
  <pre><code>var something;
  
  if (condition)
  {
      something=1;
  }
  else
  {
      something = 0;
  }
  </code></pre>
  
  <p>Even though the C# compiler could principally determine that all assignments in the current scope result in the same type, it (presently) does not and therefore generates a compile time error.</p>
  
  <p>In using <em>var</em> with LINQ, I sometimes want to initialize my result variable slightly differently depending on program conditions.  The approach I have found is to use the ?: operator, as in:</p>
  
  <pre><code>var participants = (email == null ?
                     (from part in db.Participant
                      where part.SequenceNumber == seqNr
                      select part)
                     :
                     (from part in db.Participant
                      where part.EmailAddress == email
                      select part));
  </code></pre>
  
  <p>That works well enough if I just have two possible assignments, but quickly becomes unwieldy if there are multiple possible assignments.</p>
  
  <p>Is there a more elegant approach to this?</p>