比较各种枚举列表的成分

时间:2018-12-08 08:37:01

标签: c# list

我试图在单击按钮时比较两个枚举列表的成分,并且我希望根据匹配结果接收不同的消息。

更准确地说:我手头有不同的食谱,如果我选择的食材与其中之一匹配,我会收到一条特别的消息。如果我的食材与任何东西都不匹配,我将收到一条标准消息。

这是我尝试过但无法正常工作的内容:

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

3 个答案:

答案 0 :(得分:0)

您可以使用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);
}

这将遍历RecipeList中的所有项目,并检查该项目的Recipe是否等于selectedPotion.MyIngredients。如果是这样,它将在当前项目上调用DrinkEffect()方法,否则它将显示“不喜欢任何东西!”-MessageBox。

一些评论:

  • recipe.Recipe看起来很错误,也许要进行更精确的命名
  • 代码当前无法检查selectedPotion是否为null,我认为有可能发生NullReferenceException

答案 1 :(得分:0)

基于答案,他的最终解决方案是:(我仍然收到“ 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;      
    }  

答案 2 :(得分:0)

这是最新的:

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