Skip to content
Advertisement

The LINQ expression could not be translated. Translation of method failed

I want to return list of ItemNos (string), even if few characters match and even if characters match in different place. For eg: If I pass in “appl” or “apole” or “daple”. I want DB to list “apple”.

Tried using levenshteinDistance: (Checking for strings with less than 2 edit).

Calling Compute method for finding edit distance – using this link as reference: “https://thedeveloperblog.com/levenshtein”

public async Task<IEnumerable<string>> GetItemNo(string itemNumber)
    {
        return await itemDbContext.Item
                     .Where(p => Compute(p.ItemNo, itemNumber) < 2)
                     .Select(p => p.ItemNo).ToListAsync();

    }

 

returns InvalidOperationException error:

InvalidOperationException: The LINQ expression 'DbSet<Item>()
.Where(s => ItemRepository.Compute(
s: s.ItemNo,
t: itemNumber) < 2)' could not be translated. Additional information: 
Translation of method 
'ItemApp.Infrastructure.Repository.ItemRepository.Compute' failed. If 
this method can be mapped to your custom function, see 
https://go.microsoft.com/fwlink/?linkid=2132413 for more information. 
Either rewrite the query in a form that can be translated, or switch to 
client evaluation explicitly by inserting a call to 'AsEnumerable', 
'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.

May I know where I am going wrong. Is there a problem with how I call ‘Compute’ method in my code.

Advertisement

Answer

To compute this on locally, change to

public async Task<IEnumerable<string>> GetItemNo(string itemNumber)
{
    var itemNumbers = await itemDbContext.Item.Select(p => p.ItemNo).ToListAsync();
    return itemNumbers.Where(itemNo => Compute(itemNo , itemNumber) < 2);
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement