【发布时间】:2009-01-13 14:45:00
【问题描述】:
使用 Expression.Call 时方法“Any”采用什么类型和参数?
我有一个内部表达式和一个外部表达式,我想与 Any 一起使用。表达式是以编程方式构建的。
内部(有效):
ParameterExpression tankParameter = Expression.Parameter(typeof(Tank), "t");
Expression tankExpression = Expression.Equal(
Expression.Property(tankParameter, "Gun"),
Expression.Constant("Really Big"));
Expression<Func<Tank, bool>> tankFunction =
Expression.Lambda<Func<Tank, bool>>(tankExpression, tankParameter);
外部(看起来正确):
ParameterExpression vehicleParameter = Expression.Parameter(typeof(Vehicle), "v");
Expression vehicleExpression = Expression.Lambda(
Expression.Property(
vehicleParameter,
typeof(Vehicle).GetProperty("Tank")),
vehicleParameter);
这给了我两种表达方式:
v => v.Tank
t => t.Gun == "Really Big";
而我正在寻找的是:
v => v.Tank.Any(t => t.Gun == "Really Big");
我正在尝试使用 Expression.Call 方法来使用“Any”。 1. 这是正确的做法吗? 2.以下抛出异常, “'System.Linq.Queryable' 类型上没有方法 'Any' 与提供的参数兼容。”
我是这样称呼 Any 的:
Expression any = Expression.Call(
typeof(Queryable),
"Any",
new Type[] { tankFunction.Body.Type }, // this should match the delegate...
tankFunction);
Any 调用是如何从 vehicleExpression 链接到 tankFunction 的?
【问题讨论】: