title: Return DbSet based on selection
body: <p>I'm writing an ASP.NET Core MVC web app that is a tool for handling a parts database.  What I want is for the user to select a <em>Part</em> and then that will do some action, like delete that part from it's DB.  However, I want this to be a generic action used by all the parts.  </p>
  
  <p>I have a class hierarchy which is:</p>
  
  <ul>
  <li>Part
  
  <ul>
  <li>PartA</li>
  <li>PartB</li>
  </ul></li>
  </ul>
  
  <p>What I need is some method that I can call that will get the DbSet that my part belongs to.  This is an example of what I'm looking to do:</p>
  
  <p><strong>Models</strong> </p>
  
  <pre><code>public class Part
  {
      public Nullable&lt;int&gt; ID { get; set; }
      public string Brand { get; set; }
  }
  
  public class PartA : Part
  {
      public int Length { get; set; }
      public List&lt;Image&gt; Images { get; set; }
  }
  
  public class PartB : Part
  {
      public int Durability { get; set; }
  }
  
  public class Image
  {
      public Nullable&lt;int&gt; ID { get; set; }
      public string ImagePath { get; set; }
  }
  </code></pre>
  
  <p><strong>PartsDbContext</strong></p>
  
  <pre><code>public class PartsDbContext : DbContext
  {
      public DbSet&lt;PartA&gt; PartAs { get; set; }
      public DbSet&lt;PartB&gt; PartBs { get; set; }
  }
  </code></pre>
  
  <p><strong>PartsController</strong></p>
  
  <pre><code>public IActionResult DeletePart (string partType, int id)
  {
      var partSet = GetDbSet(partType);
      var part partSet.FirstOrDefault(e =&gt; e.ID == id);
  
      if (part != null)
      {
          partSet.Remove(part);
          _context.SaveChanges();
      }
  }
  
  //function to find and return DbSet of the selected type
  private DbSet&lt;Part&gt; GetDbSet (string partType)
  {
      switch (partType)
      {
          case "PartA":
              return _context.PartAs;
          case "PartB":
              return _context.PartBs;
      }
      return null;
  }
  </code></pre>
  
  <p>Now obviously this doesn't work because the compiler will complain that: </p>
  
  <blockquote>
    <p>You can't convert type DbSet&lt;PartA&gt; to type DbSet&lt;Part&gt;</p>
  </blockquote>
  
  <p>Anyone know how I might go about doing this?</p>