1
我想使用Levenshtein距離Linq選擇查詢(如下所示)它會引發異常。Linq to Entities:不認識的方法
IEnumerable<Host> closeNeighbours = (from h in _dbContext.People
let lD = Utilities.LevenshteinDistance(lastName, h.LastName)
let length = Math.Max(h.LastName.Length, LastName.Length)
let score = 1.0 - (double)lD/length
where score > fuzziness
select h);
public static int LevenshteinDistance(string src, string dest)
{
int[,] d = new int[src.Length + 1, dest.Length + 1];
int i, j, cost;
char[] str1 = src.ToCharArray();
char[] str2 = dest.ToCharArray();
for (i = 0; i <= str1.Length; i++)
{
d[i, 0] = i;
}
for (j = 0; j <= str2.Length; j++)
{
d[0, j] = j;
}
for (i = 1; i <= str1.Length; i++)
{
for (j = 1; j <= str2.Length; j++)
{
if (str1[i - 1] == str2[j - 1])
cost = 0;
else
cost = 1;
d[i, j] =
Math.Min(
d[i - 1, j] + 1, // Deletion
Math.Min(
d[i, j - 1] + 1, // Insertion
d[i - 1, j - 1] + cost)); // Substitution
if ((i > 1) && (j > 1) && (str1[i - 1] ==
str2[j - 2]) && (str1[i - 2] == str2[j - 1]))
{
d[i, j] = Math.Min(d[i, j], d[i - 2, j - 2] + cost);
}
}
}
return d[str1.Length, str2.Length];
}
它似乎沒有工作。任何選擇?
例外: System.NotSupportedException是由用戶代碼未處理 消息= LINQ實體無法識別方法「的Int32編輯距離(System.String,System.String)」方法,和這種方法不能被翻譯成存儲表達。 Source = System.Data.Entity
非常感謝。它確實有效。讚賞。 – 2011-12-20 05:46:11