title: Can I return a collection of multiple Derived Types from Dapper query
body: <p>I have a class structure similar to this:</p>
  
  <pre><code>public abstract class Device
  {
      public int DeviceId { get; set; }
      //Additional Properties
  }
  
  public class DeviceA : Device
  {
      //Specific Behaviour
  }
  
  public class DeviceB : Device
  {
      //Specific Behaviour
  }
  </code></pre>
  
  <p>I need to retrieve a list of Devices, or a single Device which is instantiated as the appropriate derived type (based upon a Type value in the Device Record in the DB). That is, the collection of <code>Device</code> objects should contain a number of objects with different Types, all of which are derived from <code>Device</code>.</p>
  
  <p>I have implemented this the following way, but something just doesn't feel right about it.</p>
  
  <pre><code>public static IEnumerable&lt;Device&gt; AllDevices()
  {
      using (var connection = CreateConnection())
      {
          connection.Open();
          return connection.Query&lt;dynamic&gt;("SELECT * FROM Device").Select&lt;dynamic, Device&gt;(d =&gt;
              {
                  Device device = null;
                  if (d.DeviceTypeID == 1)
                      device = new DeviceA();
                  else if (d.DeviceTypeID == 2)
                      device = new DeviceB();
                  else throw new Exception("Unknown Device");
                  device.DeviceId = d.DeviceID;
                  return device;
              });
      }
  }
  </code></pre>
  
  <p>Is this the correct way to achieve this using Dapper, or is there a better approach?</p>