Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6961955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:35:51+00:00 2026-05-27T15:35:51+00:00

I am trying to use Levenshtein Distance Linq select query (as shown below) it

  • 0

I am trying to use Levenshtein Distance Linq select query (as shown below) it throws an exception.

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];
}

It does not seem to work. Any alternative?

Exception:
System.NotSupportedException was unhandled by user code
Message=LINQ to Entities does not recognize the method ‘Int32 LevenshteinDistance(System.String, System.String)’ method, and this method cannot be translated into a store expression.
Source=System.Data.Entity

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:35:52+00:00Added an answer on May 27, 2026 at 3:35 pm

    You cannot use that function in a entity framework query, as EF will not be able to translate it to the appropriate TSQL. You’ll have to bring the source sequence into memory, allowing whatever filters are applicable at the database, and then perform the rest in linq-to-objects. It’s only a subtle change.

    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; 
    

    Everything prior to AsEnumerable() will happen at the database. If there are filters generally applicable to People, you can use those prior to the AsEnumerable() invocation. Example

    var mixedQuery = db.People
                         .Where(dbPredicate).OrderBy(dbOrderSelector) // at the database
                         .AsEnumerable() // pulled into memory
                         .Where(memoryPredicate).OrderBy(memoryOrderSelector);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying use the code shown below to plot in such a way
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I am trying use Date Picker and on Select i want to display the
I am trying use std::copy to copy from two different iterator. But during course
I'm trying use to selenium with firefox on CentOS from command line like this:
i'm trying use webview to load a image from sdcard i use this path
I'm trying use jquery to remove a class from an element (not knowing ahead
I was trying use a set of filter functions to run the appropriate routine,
I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on
I am trying use a Java Uploader in a ROR app (for its ease

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.