我正在尝试在LINQ to Objects中模拟LIKE
运算符。这是我的代码:
List<string> list = new List<string>();
list.Add("line one");
list.Add("line two");
list.Add("line three");
list.Add("line four");
list.Add("line five");
list.Add("line six");
list.Add("line seven");
list.Add("line eight");
list.Add("line nine");
list.Add("line ten");
string pattern = "%ine%e";
var res = from i in list
where System.Data.Linq.SqlClient.SqlMethods.Like(i, pattern)
select i;
它没有得到我的结果,因为System.Data.Linq.SqlClient.SqlMethods.Like
仅用于转换为SQL。
在LINQ to Objects世界中是否存在与sql LIKE
运算符类似的内容?
答案 0 :(得分:18)
我不知道哪一个很容易存在,但如果你熟悉正则表达式,你可以写自己的:
using System;
using System.Text.RegularExpressions;
public static class MyExtensions
{
public static bool Like(this string s, string pattern, RegexOptions options = RegexOptions.IgnoreCase)
{
return Regex.IsMatch(s, pattern, options);
}
}
然后在你的代码中:
string pattern = ".*ine.*e";
var res = from i in list
where i.Like(pattern)
select i;
答案 1 :(得分:7)
此代码段将模仿Sql LIKE的行为和语法。您可以将它包装到您自己的lambda或扩展方法中,以便在Linq语句中使用:
public static bool IsSqlLikeMatch(string input, string pattern)
{
/* Turn "off" all regular expression related syntax in
* the pattern string. */
pattern = Regex.Escape(pattern);
/* Replace the SQL LIKE wildcard metacharacters with the
* equivalent regular expression metacharacters. */
pattern = pattern.Replace("%", ".*?").Replace("_", ".");
/* The previous call to Regex.Escape actually turned off
* too many metacharacters, i.e. those which are recognized by
* both the regular expression engine and the SQL LIKE
* statement ([...] and [^...]). Those metacharacters have
* to be manually unescaped here. */
pattern = pattern.Replace(@"\[", "[").Replace(@"\]", "]").Replace(@"\^", "^");
return Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase);
}
一种粗糙的扩展方法,可以像IEnumerable<T>.Where
方法一样工作:
public static IEnumerable<T> Like<T>(this IEnumerable<T> source, Func<T, string> selector, string pattern)
{
return source.Where(t => IsSqlLikeMatch(selector(t), pattern));
}
这反过来允许您像这样格式化您的语句:
string pattern = "%ine%e";
var res = list.Like(s => s, pattern);
修改强> 一个改进的实现,任何人都应该偶然发现并想要使用此代码。它为每个项目转换并编译一次正则表达式,并且从上面的LIKE到regex的转换有一些错误。
public static class LikeExtension
{
public static IEnumerable<T> Like<T>(this IEnumerable<T> source, Func<T, string> selector, string pattern)
{
var regex = new Regex(ConvertLikeToRegex(pattern), RegexOptions.IgnoreCase);
return source.Where(t => IsRegexMatch(selector(t), regex));
}
static bool IsRegexMatch(string input, Regex regex)
{
if (input == null)
return false;
return regex.IsMatch(input);
}
static string ConvertLikeToRegex(string pattern)
{
StringBuilder builder = new StringBuilder();
// Turn "off" all regular expression related syntax in the pattern string
// and add regex begining of and end of line tokens so '%abc' and 'abc%' work as expected
builder.Append("^").Append(Regex.Escape(pattern)).Append("$");
/* Replace the SQL LIKE wildcard metacharacters with the
* equivalent regular expression metacharacters. */
builder.Replace("%", ".*").Replace("_", ".");
/* The previous call to Regex.Escape actually turned off
* too many metacharacters, i.e. those which are recognized by
* both the regular expression engine and the SQL LIKE
* statement ([...] and [^...]). Those metacharacters have
* to be manually unescaped here. */
builder.Replace(@"\[", "[").Replace(@"\]", "]").Replace(@"\^", "^");
// put SQL LIKE wildcard literals back
builder.Replace("[.*]", "[%]").Replace("[.]", "[_]");
return builder.ToString();
}
}
答案 2 :(得分:5)
您必须使用Regex作为模式,然后使用扩展方法Where
来迭代并找到匹配项。
所以你的代码应该像这样结束:
string pattern = @".*ine.*e$";
var res = list.Where( e => Regex.IsMatch( e, pattern));
如果您不熟悉Regex,请阅读:
前0个或多个字符(。*)后跟 ine (ine),然后是0个或更多个字符(。* )然后 e (e), e 应该是字符串($)<的结尾/强>
答案 3 :(得分:1)
<强> 1。使用String.StartsWith或String.Endswith
编写以下查询:
var query = from c in ctx.Customers
where c.City.StartsWith("Lo")
select c;
will generate this SQL statement:
SELECT CustomerID, CompanyName, ...
FROM dbo.Customers
WHERE City LIKE [Lo%]
这正是我们想要的。与String.EndsWith相同。
但是,我们想用城市名称“L_n%”查询客户是什么? (以大写'L'开头,而不是某个字符,而不是'n',而不是名字的其余部分)。使用查询
var query = from c in ctx.Customers
where c.City.StartsWith("L") && c.City.Contains("n")
select c;
generates the statement:
SELECT CustomerID, CompanyName, ...
FROM dbo.Customers
WHERE City LIKE [L%]
AND City LIKE [%n%]
这不是我们想要的,也有点复杂。
<强> 2。使用SqlMethods.Like方法
深入System.Data.Linq.SqlClient
命名空间,我找到了一个名为SqlMethods的小助手类,在这种情况下它非常有用。 SqlMethods有一个名为Like的方法,可以在Linq to SQL查询中使用:
var query = from c in ctx.Customers
where SqlMethods.Like(c.City, "L_n%")
select c;
此方法获取要检查的字符串表达式(在此示例中为客户的城市),并提供与要在SQL中编写LIKE子句的方式相同的测试模式。
使用上面的查询生成了所需的SQL语句:
SELECT CustomerID, CompanyName, ...
FROM dbo.Customers
WHERE City LIKE [L_n%]
来源:http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/10/16/linq-to-sql-like-operator.aspx
答案 4 :(得分:0)
我不知道它是否存在,但这是使用我制作的Knuth-Morris-Pratt算法的扩展方法的实现。
public static IEnumerable<T> Like<T>(this IEnumerable<T> lista, Func<T, string> type, string pattern)
{
int[] pf = prefixFunction(pattern);
foreach (T e in lista)
{
if (patternKMP(pattern, type(e), pf))
yield return e;
}
}
private static int[] prefixFunction(string p)
{
int[] pf = new int[p.Length];
int k = pf[0] = -1;
for (int i = 1; i < p.Length; i++)
{
while (k > -1 && p[k + 1] != p[i])
k = pf[k];
pf[i] = (p[k + 1] == p[i]) ? ++k : k;
}
return pf;
}
private static bool patternKMP(string p, string t, int[] pf)
{
for (int i = 0, k = -1; i < t.Length; i++)
{
while (k > -1 && p[k + 1] != t[i])
k = pf[k];
if (p[k + 1] == t[i])
k++;
if (k == p.Length - 1)
return true;
}
return false;
}