.net Linq到实体:无法识别该方法

.net Linq到实体:无法识别该方法,.net,linq,entity-framework-4,.net,Linq,Entity Framework 4,我尝试使用Levenshtein距离Linq select查询(如下所示),它会引发异常 IEnumerable<Host> closeNeighbours = (from h in _dbContext.People let lD = Utilities.LevenshteinDistance(lastName, h.LastName)

我尝试使用Levenshtein距离Linq select查询(如下所示),它会引发异常

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];
}
IEnumerable closenextriends=(来自_dbContext.People中的h)
设lD=Utilities.LevenshteinDistance(lastName,h.lastName)
设length=Math.Max(h.LastName.length,LastName.length)
让分数=1.0-(双倍)lD/长度
其中分数>模糊性
选择h);
公共静态int-levenshteindication(字符串src、字符串dest)
{
int[,]d=新int[src.Length+1,dest.Length+1];
int i,j,成本;
char[]str1=src.ToCharArray();
char[]str2=dest.ToCharArray();

对于(i=0;i您不能在实体框架查询中使用该函数,因为EF将无法将其转换为适当的TSQL。您必须将源序列放入内存,允许在数据库中应用任何过滤器,然后在linq中对对象执行其余操作。这只是一个细微的更改

var closeNeighbors = from h in db.People.AsEnumerable() // bring into memory
                     // query continued below as linq-to-objects
                     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; 
AsEnumerable()
之前的所有操作都将在数据库中进行。如果存在通常适用于
人员的筛选器,则可以使用
AsEnumerable()
调用之前的筛选器。例如

var mixedQuery = db.People
                     .Where(dbPredicate).OrderBy(dbOrderSelector) // at the database
                     .AsEnumerable() // pulled into memory
                     .Where(memoryPredicate).OrderBy(memoryOrderSelector);