Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为(x=>;listOfInts.Contains(x.listofinitstocheck))创建谓词生成器_C#_Linq_Reflection_Predicates - Fatal编程技术网

C# 为(x=>;listOfInts.Contains(x.listofinitstocheck))创建谓词生成器

C# 为(x=>;listOfInts.Contains(x.listofinitstocheck))创建谓词生成器,c#,linq,reflection,predicates,C#,Linq,Reflection,Predicates,我试图构建一个谓词生成器,它返回一个谓词,检查一个int列表是否包含另一个int列表。到目前为止,我有这个 public static Expression<Func<T, bool>> DynamicIntContains<T, TProperty>(string property, IEnumerable<TProperty> items) { var pe = Expression.Parameter(typeof(T));

我试图构建一个谓词生成器,它返回一个谓词,检查一个int列表是否包含另一个int列表。到目前为止,我有这个

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);
}
这是因为我从静态方法调用它吗?还是我试图错误地访问typeof(列表)上的方法?谢谢

给你:

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();
    }
}
类程序
{
静态void Main(字符串[]参数)
{
var t1=新列表{1,3,5};
var t2=新列表{1,51};
var s=新列表{1,2,3,4,5,6,7,8,9};
控制台写入线(s.Contains(t1));
控制台写入线(s.Contains(t2));
}
}
公共静态类扩展
{
公共静态bool包含(此列表源、列表目标)
{
return!target.Except(source.Any();
}
}
和一般形式:

public static bool Contains<T>(this List<T> source, List<T> target)
{
    return !target.Except(source).Any();
}
公共静态bool包含(此列表源,列表目标)
{
return!target.Except(source.Any();
}
更一般的是:

public static bool Contains<T>(this IEnumerable<T> source, IEnumerable<T> target)
{
    return !target.Except(source).Any();
}
公共静态bool包含(此IEnumerable源、IEnumerable目标)
{
return!target.Except(source.Any();
}
给你:

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();
    }
}
类程序
{
静态void Main(字符串[]参数)
{
var t1=新列表{1,3,5};
var t2=新列表{1,51};
var s=新列表{1,2,3,4,5,6,7,8,9};
控制台写入线(s.Contains(t1));
控制台写入线(s.Contains(t2));
}
}
公共静态类扩展
{
公共静态bool包含(此列表源、列表目标)
{
return!target.Except(source.Any();
}
}
和一般形式:

public static bool Contains<T>(this List<T> source, List<T> target)
{
    return !target.Except(source).Any();
}
公共静态bool包含(此列表源,列表目标)
{
return!target.Except(source.Any();
}
更一般的是:

public static bool Contains<T>(this IEnumerable<T> source, IEnumerable<T> target)
{
    return !target.Except(source).Any();
}
公共静态bool包含(此IEnumerable源、IEnumerable目标)
{
return!target.Except(source.Any();
}
这是解决方案

    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);
    }
公共静态表达式DynamicContains(字符串属性、IEnumerable项、对象源、PropertyInfo PropertyInfo)
{
var pe=表达式参数(typeof(T));
var me=Expression.Property(pe,Property.Singularise());
var ce=Expression.Constant(propertyInfo.GetValue(source,null),typeof(List));
var convertExpression=Expression.Convert(me,typeof(int));
var call=Expression.call(ce,“Contains”,新类型[]{},convertExpression);
返回表达式.Lambda(call,pe);
}
这是基于列表名是其成员的复数的假设。在Expression.Call中,我传递了一个typeof(List),正确的方法是传入类型[]。似乎还需要将MemberExpression转换为特定类型的常量。谢谢。

这是解决方案

    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);
    }
公共静态表达式DynamicContains(字符串属性、IEnumerable项、对象源、PropertyInfo PropertyInfo)
{
var pe=表达式参数(typeof(T));
var me=Expression.Property(pe,Property.Singularise());
var ce=Expression.Constant(propertyInfo.GetValue(source,null),typeof(List));
var convertExpression=Expression.Convert(me,typeof(int));
var call=Expression.call(ce,“Contains”,新类型[]{},convertExpression);
返回表达式.Lambda(call,pe);
}

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

可能的重复与那个问题毫无关系。这一个怎么样,@Dan更相关,但仍然给出了例外“其他信息:没有泛型方法”包含“on type”System.Linq.Enumerable”与提供的类型参数和参数兼容。如果该方法是非泛型的,则不应提供任何类型参数。“是否还有其他可用信息?可能的重复信息与该问题根本不相关。这一个如何,@Dan更相关,但仍然给出了异常”其他信息:没有泛型方法“包含”系统上的“类型”。Linq.Enumerable“与提供的类型参数和参数兼容。如果方法是非泛型的,则不应提供类型参数。“还有其他可用信息吗?这只是LINQ to对象的一个选项,而问题很明显是构建一个用于查询提供程序的表达式,并将其转换为SQL或其他非C#查询机制。好吧,我是这么想的,@Servy,但我可能错了。列表中没有包含可接受集合的方法。它只接受T类型的值。我想知道他是否能够将他的代码与我的代码结合起来,我的代码提供了带有IEnumerable的Contains。我对表达式还没有太多的经验,所以这有点像是瞎猜。这与构建谓词的问题无关@Servy是完全正确的。这只是LINQ to对象的一个选项,而问题是非常清楚地构建一个表达式以在查询提供程序中使用,它将被转换为SQL或其他非C#查询机制。好吧,我是这么想的,@Servy,但我可能错了。列表中没有包含可接受集合的方法。它只接受T类型的值。我想知道他是否能够将他的代码与我的代码结合起来,我的代码提供了带有IEnumerable的Contains。我对表达式还没有太多的经验,所以这有点像是瞎猜。这与构建谓词的问题无关@塞维完全正确。