我有一个包含字符串的列表,我想使用 Levenstein 算法(或任何其他算法)来检查我尝试插入数据库的新记录是否与我在数据库中已有的记录相似。算法应该遍历列表中的每一项,并与我要插入的项进行比较。如果相似度高,则断环返回。
我已经开始了,但不确定我是否走在正确的道路上。如何在 while 循环中跳出 foreach 循环?
public static bool IsSimilarValuesExist(string value)
{
bool result = false;
string valueFromList = string.Empty;
double similarityProduct = 0;
List<string> products = ServicesMail.GetProducts();
IStringMetric metric = new Levenstein();
while (metric.GetSimilarity(value, valueFromList) < 5)
{
foreach (var item in products)
{
// If current item not similar, continue
// If is similar, break from loop and assign current compareValue to similarityProduct
}
}
return result;
}
最佳答案
How to break from foreach loop in while loop?
不要。通过重构解决问题。假设您要用一个方法替换内部循环。 为了使外部循环正确,该方法的输入和输出必须是什么?现在实际编写一个具有这些语义的方法,您的问题就解决了。
关于C# foreach 在 while 循环中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38528951/