【发布时间】:2014-10-30 17:41:28
【问题描述】:
我正在尝试构建一个谓词构建器,它返回一个谓词,该谓词检查一个整数列表是否包含另一个整数列表。到目前为止我有这个
public static Expression<Func<T, bool>> DynamicIntContains<T, TProperty>(string property, IEnumerable<TProperty> items)
{
var pe = Expression.Parameter(typeof(T));
var me = Expression.Property(pe, property);
var ce = Expression.Constant(items);
var call = Expression.Call(typeof(List<int>), typeof(List<int>).GetMethod("Contains").Name, new[] { typeof(int) }, ce, me);
return Expression.Lambda<Func<T, bool>>(call, pe);
}
T 是搜索对象,它包含一个 ID 列表作为它的属性之一。 TProperty 是整数列表,属性是属性列表的名称。我得到的错误是
Additional information: No method 'Contains' exists on type 'System.Collections.Generic.List`1[System.Int32]'.
这是因为我是从静态方法调用它吗?还是我试图错误地访问 typeof(List) 上的方法?谢谢。
【问题讨论】:
-
和那个问题完全没有关系。
-
这个怎么样,@Dan stackoverflow.com/questions/16347794/…
-
这更相关,但仍然给出异常“附加信息:类型'System.Linq.Enumerable'上没有通用方法'包含'与提供的类型参数和参数兼容。没有类型参数如果方法是非泛型的,则应提供。"
-
还有更多信息吗?
标签: c# linq reflection predicates