C# n其他人!非常感谢。实际上,我应该注意到,我个人不能将IsSqlLikeMatch的实现归功于我。几年前在互联网上找到的。我能找到的最好的归属是:我认为那是原创的 List<string> list = new List<string&g
C# n其他人!非常感谢。实际上,我应该注意到,我个人不能将IsSqlLikeMatch的实现归功于我。几年前在互联网上找到的。我能找到的最好的归属是:我认为那是原创的 List<string> list = new List<string&g,c#,linq-to-objects,sql-like,C#,Linq To Objects,Sql Like,n其他人!非常感谢。实际上,我应该注意到,我个人不能将IsSqlLikeMatch的实现归功于我。几年前在互联网上找到的。我能找到的最好的归属是:我认为那是原创的 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"); li
n其他人!非常感谢。实际上,我应该注意到,我个人不能将IsSqlLikeMatch的实现归功于我。几年前在互联网上找到的。我能找到的最好的归属是:我认为那是原创的
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;
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);
}
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);
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();
}
}
string pattern = @".*ine.*e$";
var res = list.Where( e => Regex.IsMatch( e, pattern));
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%]
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%]
var query = from c in ctx.Customers
where SqlMethods.Like(c.City, "L_n%")
select c;
SELECT CustomerID, CompanyName, ...
FROM dbo.Customers
WHERE City LIKE [L_n%]
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;
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;
}