简体   繁体   English

比较各种枚举列表的成分

[英]Comparing the ingredients of various lists of enums

I am trying to compare the ingredients of two Enum-Lists on a button click and I want to receive different messages based on the match. 我试图在单击按钮时比较两个枚举列表的成分,但我想根据匹配结果接收不同的消息。

More precisely: I have different recipes at hand and if my selected ingredients match with one of them I will receive a special message. 更准确地说:我手头有不同的食谱,如果我选择的食材与其中一种相匹配,我会收到一条特别的消息。 If my ingredients don't match with anything I will receive a standard message. 如果我的食材与任何东西都不匹配,我将收到一条标准消息。

Here is what I tried but did't worked properly: 这是我尝试过但无法正常工作的内容:

public void DrinkButton_Click(object sender, RoutedEventArgs e)
{
    foreach (var recipe in RecipeList)
    {
        List<Ingredients> copy = new List<Ingredients>(selectedPotion.MyIngredients);

        if (copy.Count == recipe.Recipe.Count)
        {
            for (int i = copy.Count - 1; i >= 0; i--)
            {
                Ingredients item = selectedPotion.MyIngredients[i];

                if (recipe.Recipe.Contains(item))
                {
                    copy.Remove(item);
                }
                if (copy.Count == 0)
                {
                    recipe.DrinkEffect();
                }
            }
        }
        else
        {
            MessageBox.Show("Doesn't taste like anything!", "Announcement!", MessageBoxButton.OK, MessageBoxImage.Information);
        }
    }
}

You could use Linq's All to check if both Lists of Ingredients contain the same elements: 您可以使用Linq的All检查两个成分表是否包含相同的元素:

public void DrinkButton_Click(object sender, RoutedEventArgs e)
{
    if (selectedPotion == null)
    {
        MessageBox.Show("Please select a potion to drink", "Help Window", MessageBoxButton.OK, MessageBoxImage.Information);
        return;
    }

    foreach (var recipe in RecipeList)
    {
        bool equalIngredients = recipe.Recipe.All(selectedPotion.MyIngredients.Contains) &&
                                    recipe.Recipe.Count == selectedPotion.MyIngredients.Count;

        if (equalIngredients)
        {
            recipe.DrinkEffect();
            return;
        }
    }

    MessageBox.Show("Doesn't taste like anything!", "Announcement!",
                        MessageBoxButton.OK, MessageBoxImage.Information);
}

This will loop over all items in RecipeList and check if the item's Recipe equals selectedPotion.MyIngredients . 这将遍历RecipeList中的所有项目,并检查该项目的Recipe等于selectedPotion.MyIngredients If so it will call the DrinkEffect() method on the current item, otherwise it displays the "Doesn't taste like anything!"-MessageBox. 如果是这样,它将在当前项目上调用DrinkEffect()方法,否则它将显示“不喜欢任何东西!”-MessageBox。

A few remarks: 几点评论:

  • recipe.Recipe just looks wrong, maybe go for a more precise naming recipe.Recipe看起来不对,可能需要更精确的命名
  • The code currently doesn't check if selectedPotion is null or not, i see potential for a NullReferenceException 该代码当前不检查selectedPotion是否为null ,我认为有可能发生NullReferenceException

Based on answers his my final solution: (I still received "System.NullReferenceException: 'Object reference not set to an instance of an object." error.) 根据答案,他得出了我的最终解决方案:(我仍然收到“ System.NullReferenceException:'对象引用未设置为对象的实例。”错误。)

    public void DrinkButton_Click(object sender, RoutedEventArgs e)
    {            
        foreach (var recipe in RecipeList)
        {               
            bool equalIngredients = recipe.Recipe.All(selectedPotion.MyIngredients.Contains) && recipe.Recipe.Count == selectedPotion.MyIngredients.Count;

            if (equalIngredients)
            {
                recipe.DrinkEffect();
                goto NextStep;
            }
        }
        MessageBox.Show("Doesn't taste like anything!", "Announcement!", MessageBoxButton.OK, MessageBoxImage.Information);
        NextStep: return;      
    }  

Here is latest: 这是最新的:

    public void DrinkButton_Click(object sender, RoutedEventArgs e)
    {
        if (selectedPotion == null)
        {
            MessageBox.Show("Please select a potion to drink", "Help Window", MessageBoxButton.OK, MessageBoxImage.Information);
            return;
        }
        else
        {
            foreach (var recipe in RecipeList)
            {
                bool equalIngredients = recipe.Recipe.All(selectedPotion.MyIngredients.Contains) && recipe.Recipe.Count == selectedPotion.MyIngredients.Count;

                if (equalIngredients)
                {
                    recipe.DrinkEffect();
                    goto NextStep;
                }
            }
            MessageBox.Show("Doesn't taste like anything!", "Announcement!", MessageBoxButton.OK, MessageBoxImage.Information);
        NextStep: return;
        }
    }

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

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