简体   繁体   English

C#foreach内部while循环

[英]C# foreach inside while loop

I have a list with strings, I want to use Levenstein algorithm(or any other) to check if the new record I am trying to insert into a database is similar to what I have already in the database. 我有一个包含字符串的列表,我想使用Levenstein算法(或任何其他方法)来检查我尝试插入数据库中的新记录是否与数据库中已有的记录相似。 Algorithm should go thru every single item in the list and compare with one I want to insert. 算法应该遍历列表中的每个项目,并与我要插入的项目进行比较。 If similarity is high, then break the loop and return. 如果相似度很高,则中断循环并返回。

I have started, but not sure If i am on the right way. 我已经开始了,但是不确定我的方法是否正确。 How to break from foreach loop in while loop? 如何在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? 如何在while循环中脱离foreach循环?

Don't. 别。 Solve the problem by refactoring. 通过重构解决问题。 Suppose you were to replace the inner loop with a method. 假设您要用一个方法替换内部循环。 What would the inputs and outputs of that method have to be in order to make the outer loop correct? 为了使外循环正确,该方法的输入和输出将是什么? Now actually write a method with those semantics, and your problem is solved. 现在,实际上使用这些语义编写方法,即可解决您的问题。

add an extra variable inside your while to indicate whether to break out of the outer loop 在您的while内添加一个额外的变量,以指示是否要跳出外循环

while(...)
{
    bool shouldBreak = false
    foreach(...)
    {
        shouldBreak = true;
        break;
    }

    if (shouldBreak)
         break;
}

Sorry for a question. 对不起,一个问题。 Looked at the problem different way. 以不同的方式看问题。 Found a different solution. 找到了不同的解决方案。

        public static bool IsSimilarValuesExist(string value)
    {
        var result = false;

        double productSimilarity = 0;
        double publisherSimilarity = 0;

        List<string> products = FilterListWithFirstOrSecondCharacter(value, ServicesMail.GetAllProductNames());
        List<string> publishers = FilterListWithFirstOrSecondCharacter(value, ServicesMail.GetAllPublisherNames());

        IStringMetric metric = new Levenstein();

        foreach (var item in products)
        {
            if (metric.GetSimilarity(value, item) * 100 > 80)
            {
                productSimilarity = metric.GetSimilarity(value, item) * 100;
            }
        }

        foreach (var item in publishers)
        {
            if (metric.GetSimilarity(value, item) * 100 > 80)
            {
                publisherSimilarity = metric.GetSimilarity(value, item) * 100;
            }
        }

        var averageSimilarity = productSimilarity * publisherSimilarity / 2;
        if (averageSimilarity >= 80)
        {
            result = true;
        }

        return result;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM