简体   繁体   English

具有循环linq C#的动态where子句

[英]Dynamic where clause with loop linq c#

I have dictionary with regular expressions and a data table. 我有带有正则表达式和数据表的字典。

var data = ds.Tables["mytable"].AsEnumerable();

var regexlist = new Dictionary<string, Regex>
{
     {"PI1", new Regex(@"(-.[^I](-P[^IS]))|(-[^P].(-P[^IS]))")},
     {"SM1", new Regex("(-.[^I](-P[^IS]))|(-[^P].(-P[^IS]))")}
};

Now I want to select all rows of the data table that does not match the regular expressions in the list and also the key of the dictionary (the error code). 现在,我要选择与列表中的正则表达式不匹配的数据表的所有行,以及字典的键(错误代码)。

So far I have this: 到目前为止,我有这个:

var query = data.Select(dr => dr.Field<string>("F1"));
query = regexlist.Aggregate(query, (current, regex1) => current.Where(u => regex1.Value.IsMatch(u) ));

but I think that only the first regex is added as where clause. 但是我认为只有第一个正则表达式被添加为where子句。 And I don't know how to output the "error code" 而且我不知道如何输出“错误代码”

I hope I explained my problem clear. 我希望我能清楚地解释我的问题。

jonas 乔纳斯

整个过程看起来还不错,除了其中的表达式不能反映您的目标,您需要反转测试子句以获取与正则表达式不匹配的数据

current.Where(u => regex1.Value.IsMatch(u) == false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM