从列表中查找字符串

我列出了几种成分:

List<string> vegList = new List<string>();

其中有一些字符串,例如:“樱桃”,“黄油”,“面包”。

我的想法是拥有一本字典或类似带有食谱字符串的内容,如果该食谱中有一个或多个元素,则返回该食谱的名称。

我尝试制作字典和数组,但没有得到想要的东西。 有人可以帮忙吗?

我尝试了这个,但我不知道该怎么办了

myString = "receit pineapple cake, cherry, bread ..."
foreach(string item in vegList )
{
    if(item.Contains(myString))
       return item;
}
评论
  • SOS
    SOS 回复

    If each recipe has a vegList, or more specifically, you have a Recipe class with a List<string> vegList field, and let's say you have a List<Recipe> recipes somewhere:

    var ingredient = "cherry";
    
    var matches = recipes.FindAll(r => r.vegList.contains(ingredient));
    

    matches will be a List<Recipe> with recipes containing "cherry" as an ingredient.