2017-08-30 117 views
0

我有我的类的外部DLL,我想根据类型进行查询。我解释得更好。实体框架核心通用查询

我得到的类型与功能的GetType:

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

如果有什么办法让这样的选择:

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

假设_context是我的DbContext。

感谢

+0

我很确定像声明是你想要的_context.Set(typeof(targetType))。ToList() –

回答

1

确实存在:

var method = typeof(DbContext).GetMethod("Set").MakeGenericMethod(targetType); 
var query = method.Invoke(ctx, null) as IQueryable; 
var list = query.OfType<object>().ToList(); 
0

嗯,我做了类似的东西。
下面是一个例子

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>; 
} 
} 

合这让你开始。 从moble写的:)