【发布时间】:2016-11-03 01:57:53
【问题描述】:
我正在尝试创建一个动态 lambda exp 来过滤我列表中的一些结果,但我不知道如何创建一个动态 func
//Here I get the type of my object "Produto", but I can have other objects here...Like "Pedido" or another one.
Type type = typeof(Produto);
var param = Expression.Parameter(type, "arg");
var propName = Expression.Property(param, "Codigo");
var constP = Expression.Constant("123");
var nameStartsWith = Expression.Call(
propName,
typeof(string).GetMethod("StartsWith", new[] { typeof(string) }),
constP);
//Here I have to instantiate my Func<T, bool>
//I can't instantiate it with my variable "type" to use on my Where clausule.
//I don't know how to do this in another way, to create my lambda.
var WhereExp = Expression.Lambda<Func<type, bool>>(nameStartsWith, param);
return _uow.produto.GetAll().AsQueryable().Where(WhereExp).ToList();
【问题讨论】:
-
你能为这种未知类型创建接口吗?
-
“动态”Func 到底是什么意思?
-
如果你可以在
_uow上创建一个泛型类型的函数,我认为它会更好。 -
是什么阻止您在编译时将类型作为泛型类型传入?编译时不知道类型吗?