none
linq.Expressions把正则表达式作为筛选条件的代码是什么? RRS feed

  • 问题

  • 用户输入符合正则表达式规范的字符串,linq.Expressions怎么把这段字符串作为正则表达式对对泛型实体集合中的字符串属性进行筛选?

    List<Student> stuList = new List<Student>();
                loadStuIn(stuList); //加载测试数据的方法
                var parameter = Expression.Parameter(typeof(Student), "p");
                var left = Expression.Call(
                    Expression.Property(parameter, "Name"),
                    typeof(Regex).GetMethod("IsMatch"),
                    Expression.Constant(@"(?<=19)\d{2}\b"));
                var lambda = Expression.Lambda<Func<Student, bool>>(left, parameter);
               var _r = stuList.Where(lambda.Compile());
                foreach (var n in _r)
                {
                    Console.WriteLine(n.Name);
                }
                Console.ReadKey();


    代码运行时,

     var left = Expression.Call(
                    Expression.Property(parameter, "Name"),
                    typeof(Regex).GetMethod("IsMatch"),
                    Expression.Constant(@"(?<=19)\d{2}\b"));

    这一段会抛出"System.Reflection.AmbiguousMatchException:“发现不明确的匹配。”
    "的异常,那正确的代码是什么?



    2017年7月10日 10:36

答案

  • Hi,

    你现在是用正则表达式来匹配多个数据? 结果就会爆出这个错误,但是用具体的字符串来匹配,就没有报错? 现在是这个现象吗?

    从微软官方的文档中,我们可以看到这句话。

    >>" The exception that is thrown when binding to a member results in more than one member matching the binding criteria. This class cannot be inherited. "

    从字面理解,当绑定的结果多于一个成员,而且这个成员还满足匹配的条件,这个错误就会出来。

    现在你用正在表达式,来匹配绑定结果,这个结果肯定是多于一个,且满足匹配条件的。 这是必然会出错的。

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年7月14日 7:44
    版主

全部回复

  • Hi,

    感谢你在MSDN论坛发帖。

    你有debug你当前的工程吗?是具体在哪一行报出这个错误。你这个代码片段,我测试是不工作的。

    根据这个错误信息,当绑定到成员导致多个成员匹配绑定条件时,就会爆出这个错误。

    这边有解释的文档和实例。

    https://msdn.microsoft.com/en-us/library/system.reflection.ambiguousmatchexception(v=vs.110).aspx

    Best Regards,

    Hart<sentencetext xmlns="http://www.w3.org/1999/xhtml"></sentencetext>


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2017年7月11日 7:08
    版主
  • Hi,

    感谢你在MSDN论坛发帖。

    你有debug你当前的工程吗?是具体在哪一行报出这个错误。你这个代码片段,我测试是不工作的。

    根据这个错误信息,当绑定到成员导致多个成员匹配绑定条件时,就会爆出这个错误。

    这边有解释的文档和实例。

    https://msdn.microsoft.com/en-us/library/system.reflection.ambiguousmatchexception(v=vs.110).aspx

    Best Regards,

    Hart<sentencetext xmlns="http://www.w3.org/1999/xhtml"></sentencetext>


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    问题描述更新了,异常错误提示在这一区域

    var left = Expression.Call(
                    Expression.Property(parameter, "Name"),
                    typeof(Regex).GetMethod("IsMatch"),
                    Expression.Constant(@"(?<=19)\d{2}\b"));

    2017年7月11日 7:59
  • Hi,

    根据我我的了解,我不太清楚你这样用法是不是对的。

    对于expression.call 函数的第一个参数都是instance,应该不是property.

    第二个参数是一个方法的属性。

    第三个是参数的集合。

    请检查一下你的用法,根据MSDN文档call的解释。

    https://msdn.microsoft.com/en-us/library/system.linq.expressions.expression.call(v=vs.110).aspx

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年7月12日 6:29
    版主
  • Hi,

    根据我我的了解,我不太清楚你这样用法是不是对的。

    对于expression.call 函数的第一个参数都是instance,应该不是property.

    第二个参数是一个方法的属性。

    第三个是参数的集合。

    请检查一下你的用法,根据MSDN文档call的解释。

    https://msdn.microsoft.com/en-us/library/system.linq.expressions.expression.call(v=vs.110).aspx

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    我这样用是对的,因为我用contain方法筛选数据时成功过

    var left = Expression.Call(
                    Expression.Property(parameter, "Name"),
                    typeof(String).GetMethod("Contains"),
                    Expression.Constant(@"sdadsa"));

    2017年7月12日 11:51
  • Hi,

    你现在是用正则表达式来匹配多个数据? 结果就会爆出这个错误,但是用具体的字符串来匹配,就没有报错? 现在是这个现象吗?

    从微软官方的文档中,我们可以看到这句话。

    >>" The exception that is thrown when binding to a member results in more than one member matching the binding criteria. This class cannot be inherited. "

    从字面理解,当绑定的结果多于一个成员,而且这个成员还满足匹配的条件,这个错误就会出来。

    现在你用正在表达式,来匹配绑定结果,这个结果肯定是多于一个,且满足匹配条件的。 这是必然会出错的。

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年7月14日 7:44
    版主