我有一个场景,我需要使用linq(与nhibernate)来打一个动态查询。最后的查询应该是这样的:调用任何内部表达式并通过代理
long[] values = { ... };
var result = Queryable<Entity>.Where(x => x.Documents.Any(d => values.Contains(d.Id)))
.ToList();
通用Entity
和财产Documents
可以改变,它会通过某个用户的配置来定义。收集类型Documents
是ICollection<T>
,其中T
是Document
类型。我正在尝试创建Expression
树来动态定义这些语句,但我遇到了一些问题。看看我试过的代码和评论。
我创建这个函数返回我想要的Any
方法内使用delagate:
public static Func<T, bool> GetFunc<T>(long[] values)
where T : Entity
{
return x => values.Contains(x.Id);
}
,我使用表达式类来作出这样的表达(见代码和注释):
// define my parameter of expression
var parameter = Expression.Parameter(typeof(T), "x");
// I get an array of IDs (long) as argument and transform it on an Expression
var valuesExpression = Expression.Constant(values);
// define the access to my collection property. propertyFilter is propertyinfo for the `Documents` of the sample above.
// I get an expression to represent: x.Documents
var collectionPropertyExpression = Expression.Property(parameter, propertyFilter);
// get the T generic type of the ICollection<T> from propertyFilter. I get the `Documents` of sample above.
var entityFilterType = propertyFilter.PropertyType.GetGenericArguments()[0];
// get the definition of `Any` extension method from `Enumerable` class to make the expression
var anyMethod = typeof(Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public)
.First(x => x.Name == "Any" && x.GetParameters().Length == 2)
.MakeGenericMethod(entityFilterType);
// get a methodBase for GetFunc to get the delagete to use inside the Any
// using the `Document` generic type
var collectionBody = typeof(LookUpHelper).GetMethod("GetFunc", BindingFlags.Public | BindingFlags.Static)
.MakeGenericMethod(entityFilterType);
// call the any passing the collection I need and convert it to a Delegate
// I get something like: x => values.Contains(x.Id) ... where x if the `Document`
var func = (Delegate)collectionBody.Invoke(null, new object[] { values });
// get the func as an expression .. maybe the problem is here
var funcExpression = Expression.Constant(func);
// call the any passing the collection and my delagate as arguments
var f = Expression.Call(anyMethod, collectionPropertyExpression, funcExpression);
// I already have an expression and concatenate it using `AndAlso` operator.
body = Expression.AndAlso(body, f);
// finally, I built up to lambda expression and apply it on my queryable
var filterExpression = Expression.Lambda<Func<T, bool>>(body, parameter);
var result = Queryable.Where(filterExpression).ToList();
它执行直到通过ToList
方法执行查询。我收到以下错误:
Could not parse expression 'x.Documents.Any(value(System.Func`2[Project.Document,System.Boolean]))': The object of type 'System.Linq.Expressions.ConstantExpression' cannot be converted to type 'System.Linq.Expressions.LambdaExpression'. If you tried to pass a delegate instead of a LambdaExpression, this is not supported because delegates are not parsable expressions.
我不知道我在做什么错。有人可以帮助我吗?
谢谢。
我已修复它,而不是按预期工作。我对Func和Expression感到困惑,我是盲目的,正在寻找如何将这个Func转换为Expression,而我甚至不想改变这种非动态方法。谢谢大卫。 –
这是一个容易陷入陷阱。乐于帮助! –