title: How to cast DbSet<T> to List<T>
body: <p>Given the following simplified Entity Framework 6 context, I am trying to populate a List with the entities but having problems with how to cast (I believe) via reflection.</p>
  
  <pre><code>public class FooContext : DbContext
  {
     public virtual IDbSet&lt;FooClass&gt; Foo { get; set; }    
     //...
  }
  
  public class FooClass
  {
      public int Id{ get; set; }
      public string Name {get; set; }
      //...    
  }
  
  public main()
  {
       using (var context = new FooContext())
       {
           var sets = typeof(FooContext).GetProperties().Where(pi =&gt; pi.PropertyType.IsInterface &amp;&amp; pi.PropertyType.GetGenericTypeDefinition().ToString().ToLower().Contains("idbset"));
  
           foreach (var set in sets)
           {
               //When I debug and enumerate over 'value' the entire results are shown so I believe the reflection part is OK.
               var value = set.GetValue(context, null);
  
               //Always returns null. How can I cast DbSet&lt;T&gt; to List&lt;object&gt; or List&lt;T&gt;? 
               var list = value as List&lt;object&gt;();
  
               //...
           }
       }
  }
  </code></pre>
  
  <p>I'm doing this for utility method for some integration testing I am doing. I am trying to do this without using direct inline SQL calls (using SqlConnection and SqlCommand etc) to access the database (as the datastore may change to Oracle etc).</p>