Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.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# 使用Lambda表达式进行正则表达式匹配_C#_Regex_Linq_Lambda - Fatal编程技术网

C# 使用Lambda表达式进行正则表达式匹配

C# 使用Lambda表达式进行正则表达式匹配,c#,regex,linq,lambda,C#,Regex,Linq,Lambda,我正在尝试使用LINQtoSQL实现(*,?)的通配符搜索功能。到目前为止,我想尝试使用正则表达式,因为我们编写的代码将很短并且易于管理。这是我的 string kw=_keyword.Replace("*",".*").Replace("?","."); var predicate = PredicateBuilder.True<DAL.RequestAttribute>(); Regex reg=new Regex("^"+kw+"$");

我正在尝试使用LINQtoSQL实现(*,?)的通配符搜索功能。到目前为止,我想尝试使用正则表达式,因为我们编写的代码将很短并且易于管理。这是我的

  string kw=_keyword.Replace("*",".*").Replace("?",".");
        var predicate = PredicateBuilder.True<DAL.RequestAttribute>();
        Regex reg=new Regex("^"+kw+"$");
        predicate=predicate &&(reg.IsMatch(ra=>ra.AttributeValue));
string kw=_关键字。替换(“*”,“*”)。替换(“?”,”);
var predicate=PredicateBuilder.True();
正则表达式reg=新正则表达式(“^”+kw+“$”);
谓词=谓词&(reg.IsMatch(ra=>ra.AttributeValue));
因此,这里给出了一个编译错误,即“无法将lambda表达式转换为'string'类型,因为它不是委托类型”

虽然如果我让它编译并运行,会有一些变通方法,但我会遇到这个运行时错误

方法“Boolean IsMatch(System.String)”不支持转换为SQL

我这里有两个问题 1.我是否在考虑使用正则表达式实现我的通配符?如果没有,哪种方法更有效? 2.如何解决此错误


谢谢,您可以通过使用SqlMethods来模拟SQL的LIKE运算符。LIKE:

请确保使用适当的通配符/标记:

更新: 不能将正则表达式与简单SQL一起使用。因为您实际上是通过LINQ构建SQL语句,所以同样的规则也适用。尽管如此,仍然不太清楚您在哪里使用您提供的示例代码访问LINQ

我通常希望看到如下情况:

var results = 
  from Something in SomeLinqContext 
  where SqlMethods.Like(Something.Value, kw);

经过研究,我找到了问题的答案。感谢文陶尔的建议。解决方案是类似的,但只是有点不同。它的代码是什么

string kw=_keyword.Replace("*","%").Replace("?","_");
var predicate = PredicateBuilder.True<DAL.RequestAttribute>();
predicate = (ra => SqlMethods.Like(ra.AttributeValue, kw) && <Any other boolean conditions>
string kw=_关键字。替换(“*”,“%”)。替换(“?”,“”);
var predicate=PredicateBuilder.True();
谓词=(ra=>SqlMethods.Like(ra.AttributeValue,kw)&&

因此,它归结为如何使用谓词生成器类从SQLMethods.like方法中生成表达式。

由于第一个错误再次出现,此SQL的like方法也不起作用。下面是我尝试的代码字符串kw=_关键字。Replace(“*”,“%”)。Replace(“?”,“”);var predicate=PredicateBuilder.True();Regex reg=new Regex(“^”+kw+“$”);predicate=predicate&&(System.Data.Linq.SqlClient.SqlMethods.Like((ra=>ra.AttributeValue),kw));我在最后一行中得到了所述的错误..更新我的答案以澄清一些问题。在更为手动的处理中,我得到了“*”字符串[]的代码,如下所示_searchParts=_关键字.Split('*');if(_searchParts.Length==2){if(string.IsNullOrEmpty(_searchParts[0]){if(_fieldName==null)谓词=(ra=>ra.AttributeValue.EndsWith(_searchParts[1])&&ra.Attribute.LibID==_LibID);else谓词=(ra=>ra.AttributeValue.EndsWith(_searchParts[1])&&ra.Attribute.LibID==\u LibID&&ra.Attribute.Name==\u fieldName);}这一切正常,我正在使用位于“”的Predicatebuilder类在何处/如何使用谓词?在使用LINQ查询时,我使用谓词过滤记录。因此..PFB一个简单的类比..我按如下方式构建谓词..predicate=predicatebuilder.True();并在此基础上添加条件..predicate=(ra=>ra.AttributeValue.Endswith(“abc”)&&ra.Attribute.LibID=26;现在我在LINQ查询中使用这个谓词作为DAL.LibMgmtDataClassesDataContext dCtx=DAL.LibMgmtDataClassesDataContext.GetDataContext();返回(从dCtx.RequestAttributes.Where(requestAttributesExpr)选择ra.Request.RequestID.Distinct().ToList();