1

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
  • I'm pretty sure that like statement is what you want _context.Set(typeof(targetType)).ToList() Commented Aug 30, 2017 at 15:57

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

Comments

-2

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.