2

我正在尝试构建一个谓词构建器,它返回一个谓词,该谓词检查一个整数列表是否包含另一个整数列表。到目前为止我有这个

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) 上的方法?谢谢。

4

2 回答 2

1

这是解决方案;

    public static Expression<Func<T, bool>> DynamicIntContains<T, TProperty>(string property, IEnumerable<TProperty> items, object source, PropertyInfo propertyInfo)
    {
        var pe = Expression.Parameter(typeof(T));
        var me = Expression.Property(pe, property.Singularise());
        var ce = Expression.Constant(propertyInfo.GetValue(source, null), typeof(List<int>));
        var convertExpression = Expression.Convert(me, typeof(int));
        var call = Expression.Call(ce, "Contains", new Type[] { }, convertExpression);
        return Expression.Lambda<Func<T, bool>>(call, pe);
    }

这消除了列表名称是其成员的复数的假设。在 Expression.Call 中,我传递了一个 typeof(List),正确的方法是传递 Type[]。似乎也需要将 MemberExpression 转换为特定类型的常量。谢谢。

于 2014-11-03T11:05:13.453 回答
-2

干得好:

class Program
{
    static void Main(string[] args)
    {
        var t1 = new List<int> {1, 3, 5};
        var t2 = new List<int> {1, 51};
        var s = new List<int> {1, 2, 3, 4, 5, 6, 7, 8, 9};
        Console.WriteLine(s.Contains(t1));
        Console.WriteLine(s.Contains(t2));
    }

}

public static class Extensions
{
    public static bool Contains(this List<int> source, List<int> target)
    {
        return !target.Except(source).Any();
    }
}

和一个通用形式:

public static bool Contains<T>(this List<T> source, List<T> target)
{
    return !target.Except(source).Any();
}

更通用的:

public static bool Contains<T>(this IEnumerable<T> source, IEnumerable<T> target)
{
    return !target.Except(source).Any();
}
于 2014-10-30T18:23:24.117 回答