相关疑难解决方法(0)

如何创建一个调用IEnumerable <TSource> .Any(...)的表达式树?

我正在尝试创建一个表示以下内容的表达式树:

myObject.childObjectCollection.Any(i => i.Name == "name");
Run Code Online (Sandbox Code Playgroud)

为清楚起见,我有以下内容:

//'myObject.childObjectCollection' is represented here by 'propertyExp'
//'i => i.Name == "name"' is represented here by 'predicateExp'
//but I am struggling with the Any() method reference - if I make the parent method
//non-generic Expression.Call() fails but, as per below, if i use <T> the 
//MethodInfo object is always null - I can't get a reference to it

private static MethodCallExpression GetAnyExpression<T>(MemberExpression propertyExp, Expression predicateExp)
{
    MethodInfo method = typeof(Enumerable).GetMethod("Any", new[]{ typeof(Func<IEnumerable<T>, Boolean>)});
    return …
Run Code Online (Sandbox Code Playgroud)

.net c# linq expression-trees

37
推荐指数
2
解决办法
1万
查看次数

标签 统计

.net ×1

c# ×1

expression-trees ×1

linq ×1