title: Inheriting a Linq to SQL class and cast the result of a linq query
body: <p>I am writing an application where we will need to extend a basic entity into a number of different things (eg employee, vehicle etc).  The design is as such that there is a Entity table and a second table with type specific values eg an employee will have an ID Number but a vehicle will have a registration number.</p>
  
  <p>I have inherited from the class entity generated in the Data Context but am having trouble with the casting in my repository.  What is the correct way of doing this?</p>
  
  <pre><code>public class cAccountEmployee : cAccountEntity
  {
      public string id_number
      {
          get
          {
              try
              {
                  return this.cAccountEntityValues.Single(e =&gt; e.type == 1).value;
              }
              catch (Exception)
              {
                  return "";
              }
          }
  
          set
          {
              try
              {
                  this.cAccountEntityValues.Single(e =&gt; e.type == 1).value = value;
              }
              catch (Exception)
              {
                  this.cAccountEntityValues.Add(new cAccountEntityValue()
                                              {
                                                  accountentity_id = this.id,
                                                  cAccountEntity = this,
                                                  type = 1,
                                                  value = value
                                              });
              }
          }
      }
  </code></pre>
  
  <p>}</p>
  
  <p>Then in my repository (does not inherit anything)</p>
  
  <pre><code>public IEnumerable&lt;cAccountEmployee&gt; All(int accountholder_id)
      {
          return db.cAccountEntities.Where(e =&gt; e.accountholder_id == accountholder_id).OrderBy(a =&gt; a.name).Cast&lt;cAccountEmployee&gt;();
      }
  
      public cAccountEmployee Single(int id)
      {
          return db.cAccountEntities.Single(a =&gt; a.id == id) as cAccountEmployee;
      }
  </code></pre>
  
  <p>The cast fails in the single method and hence I get back null.  It is my understanding you cannot define explicit or implicit operators from or to a base class?  How can I get the base class Linq result to cast up to the inherited Employee class, while still maintaining its db state so I can submit changes?</p>