[英]Entity framework core generic query
我的类有一个外部DLL,我想根据类型进行查询。 我解释得更好。
我使用函数GetType获得类型:
Type targetType = Type.GetType("...");
如果有任何方法可以像这样进行选择:
_context.Set<targetType>().ToList()
假设_context是我的DBContext。
谢谢
确实有:
var method = typeof(DbContext).GetMethod("Set").MakeGenericMethod(targetType);
var query = method.Invoke(ctx, null) as IQueryable;
var list = query.OfType<object>().ToList();
好吧,我做了类似的事情。
这是一个例子
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 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.