title: How to return dynamic return types in methods? C#
body: <p>I am having a problem with the return type of a method.</p>
  
  <p>The method returns a linq object which at present returns type tblAppointment. This method is shown below:</p>
  
  <pre><code>public tblAppointment GetAppointment(int id)
  {
      var singleAppointment = (from a in dc.tblAppointments
                                                      where a.appID == id
                                                      select a).SingleOrDefault();
      return singleAppointment;
  
  }
  </code></pre>
  
  <p>The problem is that tblAppointment is abstract and has many sub types that inherit it. When I try and return an object that is of Type "appointmentTypeA" and call the .GetType() method on it, it gives me the correct sub type, but when i try and access the properties it only allows me to access the parent properties. If i take the object and cast it to a new object of the subtype then it works and lets me access everything i need but it seems messy.</p>
  
  <pre><code>var viewSingleAppointment = appointmentRepos.GetAppointment(appointmentId);
  
  Debug.Write(viewSingleAppointment.GetType()); //returns type i want
  
  if (viewSingleAppointment is tblSingleBirthAppointment)
  {
      tblSingleBirthAppointment myApp = (tblSingleBirthAppointment)viewSingleAppointment; //need to do this to access TypeA properties for some reason
  
  }
  </code></pre>
  
  <p>Edit: I have got this working but I need to use a select statement for each appointment (about 20) and cast them to the appropriate type and retreive the properties and im not sure how to refactor this as it will be used on a few pages we are doing.</p>