c# - How to cast DbSet<T> to List<T> -
given following simplified entity framework 6 context, trying populate list entities having problems how cast (i believe) via reflection.
public class foocontext : dbcontext { public virtual idbset<fooclass> 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 => pi.propertytype.isinterface && pi.propertytype.getgenerictypedefinition().tostring().tolower().contains("idbset")); foreach (var set in sets) { //when debug , enumerate on 'value' entire results shown believe reflection part ok. var value = set.getvalue(context, null); //always returns null. how can cast dbset<t> list<object> or list<t>? var list = value list<object>(); //... } } } i'm doing utility method integration testing doing. trying without using direct inline sql calls (using sqlconnection , sqlcommand etc) access database (as datastore may change oracle etc).
idbset inherits iqueryable<tentity>, ienumerable<tentity>, iqueryable, , ienumerable, can't directly cast list way. list<tentity> of entities in dbset though using .tolist() or .tolistasync()
this creates copy of entities in memory though, should consider operating linq directly on dbset
Comments
Post a Comment