title: LINQ Conditionally Add Join
body: <p>I have a LINQ query where I'm trying to return data from 2 tables, but the tables that I join are conditional.</p>
  
  <p>This is what I'd like to do:</p>
  
  <pre><code>if (teamType == "A"){
      var query = from foo in context.People
                  join foo2 in context.PeopleExtendedInfoA
                  select foo;
  }
  else {
      var query = from foo in context.People
                  join foo2 in context.PeopleExtendedInfoB
                  select foo;
  }
  </code></pre>
  
  <p>Then later on I'm filtering the query down even further.  I obviously can't set it up this way because I won't be able to access "query" outside the if block, but it shows what I'm trying to do.  This is an example of what I'm trying to do later on with the query:</p>
  
  <pre><code>if (state != null)
  {
       query = query.Where(p =&gt; p.State == state);
  }
  
  if (query != null) {
     var queryFinal = from foo in query
           select new PeopleGrid()
           {
                Name = foo.Name,
                Address = foo.Address,
                Hobby = foo2.Hobby
           }
  }
  </code></pre>
  
  <p>What I'm trying to return is all the data from table foo and then one field from the joined table, but depending on the logic, the joined table will differ.  Both PeopleExtendedInfoA and PeopleExtendedInfoB both have the columb 'Hobby', but I have no way to access 'Hobby' from the joined table and that's the only field I need from the joined table.</p>
  
  <p>How would I go about doing this?</p>