无法翻译 LINQ 表达式。方法翻译失败

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

我想要 return ItemNos(字符串)列表,即使匹配的字符很少,即使字符在不同的地方匹配。 例如:如果我传入“appl”或“apole”或“daple”。我希望数据库列出“苹果”。

尝试使用 levenshteinDistance:(检查少于 2 个编辑的字符串)。

调用计算方法查找编辑距离 - 使用此 link 作为参考:“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 错误:

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'.

请问我哪里错了。我在代码中调用 'Compute' 方法有问题吗?

要在本地计算,请更改为

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