C#LINQ:如何处理通配符搜索?

我需要一个像这样的搜索函数:该函数接受具有多种类型通配符的所有字符串:

* or % >>> can replace one or more characters # >>> used to replace a number @ >>> used to replace an alphabet ? >>> used to replace a character (can be both number and alphabet) 

据我所知,在LINQ中有3个搜索字符串的函数:StartsWith,EndsWith和Contains。 在SQL查询中,有两种类型的LIKE:%表示一个或多个字符,_表示只有一个字符。 那么,我该如何解决这个问题呢?

非常感谢你

你可以使用正则表达式并用正则表达式替换通配符。 这是一个例子。

  IEnumerable Search(IEnumerable data, string q) { string regexSearch = q .Replace("*", ".+") .Replace("%", ".+") .Replace("#", "\\d") .Replace("@", "[a-zA-Z]") .Replace("?", "\\w"); Regex regex = new Regex(regexSearch); return data .Where(s => regex.IsMatch(s)); }