Entity framework core generic query

c# entity-framework-core

Question

I have a external DLL with my classes and I would like to make the query depending on the type. I explain better.

I get the type with the function GetType:

Type targetType = Type.GetType("...");

If there is any way to make a select like this:

_context.Set<targetType>().ToList()

Assuming that _context is my DBContext.

Thanks

1
0
8/30/2017 3:53:32 PM

Accepted Answer

Indeed there is:

var method = typeof(DbContext).GetMethod("Set").MakeGenericMethod(targetType);
var query = method.Invoke(ctx, null) as IQueryable;
var list = query.OfType<object>().ToList();
1
8/30/2017 4:02:52 PM

Popular Answer

Well i did something like that.
Here is an example

Public class repository: dbcontext
{
 public IDbset<car> Cras {get;  set;} 

Public IQueryable<T> Get<T>()
{
return this.gettype().getproberties().find(x=>.  x.propertytype== typeof(T)). Getvalue(this)  as IQueryable<T>;
}
}

Hop this get you started. Writen from moble :)



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow