如何在LINQ查询中比较Regex的字符串? [英] How to Compare strings by Regex inside a LINQ Query?
问题描述
大家好,
我有两个数据表dtRosterList和falsefields我想列出针对falsefields的dtrosterlist的所有行,其中dtrosterlist列值是一个句子你好我是一个字段列表和dtfalsefields列值有一个字符串field...所以我匹配Regex.IsMatch,因为我无法直接比较
Regex.IsMatch( Facebook链接列表,@\ bFacebook Link \b)// true
但是在我的LINQ查询中它返回false并且我得到空结果-set
Hi all,
I have two datatables dtRosterList and falsefields I want to list out all rows of dtrosterlist against falsefields ,where dtrosterlist column value is a sentence "Hello i am a list of field" and dtfalsefields column value having a single string "field"...so i am matching by Regex.IsMatch,since i can't compare directly
Regex.IsMatch("List of Facebook Link",@"\bFacebook Link\b") // true
However inside my LINQ query its returning false and i am getting empty result-set
var listTobeDeleted = dtRosterList.AsEnumerable().
Where(r => falsefields.AsEnumerable()
.Select(f => f.Field<string>("FieldName"))
.Any(fn => Regex.Match(r.Field<string>("ListName"),
@"\b"+fn+"\b",RegexOptions.IgnoreCase))).CopyToDataTable();
任何关于REGEX的帮助都将不胜感激,
谢谢!
Any help regarding REGEX would be appreciated,
Thanks!
推荐答案
两个问题使用您的代码:
- 您使用
Regex.Match
而不是Regex.IsMatch
;- 你错过了
@
pre修复第二个\ b
字符串;
Two problems with your code:
- You've used
Regex.Match
instead ofRegex.IsMatch
;- You've missed the
@
prefix on the second"\b"
string;
var listTobeDeleted = dtRosterList.AsEnumerable()
.Where(r => falsefields.AsEnumerable()
.Select(f => f.Field<string>("FieldName"))
.Any(fn => Regex.IsMatch(r.Field<string>("ListName"), @"\b" + Regex.Escape(fn) + @"\b", RegexOptions.IgnoreCase)))
.CopyToDataTable();
这篇关于如何在LINQ查询中比较Regex的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!