Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#如何将数组中的所有项目与搜索词列表匹配_C#_.net - Fatal编程技术网

C#如何将数组中的所有项目与搜索词列表匹配

C#如何将数组中的所有项目与搜索词列表匹配,c#,.net,C#,.net,我有一门食谱课: public class Recipe { public string Id { get; set; } public string RecipeTitle { get; set; } public string ChefName { get; set; } public List<string> HashTags { get; set; } public string Ingredients { get; set; } }

我有一门食谱课:

public class Recipe
{
    public string Id { get; set; }
    public string RecipeTitle { get; set; }
    public string ChefName { get; set; }
    public List<string> HashTags { get; set; }
    public string Ingredients { get; set; }
}
公共类配方
{
公共字符串Id{get;set;}
公共字符串RecipeTle{get;set;}
公共字符串ChefName{get;set;}
公共列表HashTags{get;set;}
公共字符串{get;set;}
}
用户可以在搜索框中输入搜索词列表,我想返回与所有搜索词匹配的菜谱。以下是我目前的代码:

public Recipe[] SearchRecipes(string[] searchTerms, Recipe[] recipes)
{
    var matches = new List<Recipe>();

    foreach (var recipe in recipes)
    {

        if (searchTerms.Contains(recipe.ChefName, StringComparer.OrdinalIgnoreCase))
        {
            matches.Add(recipe);
        }

        foreach (var tag in recipe.Hashtags)
        {
            if (searchTerms.Contains(tag, StringComparer.OrdinalIgnoreCase))
            {
                matches.Add(recipe);
            }
        }

        foreach (var title in recipe.RecipeTitle)
        {
            if (searchTerms.Contains(title, StringComparer.OrdinalIgnoreCase))
            {
                matches.Add(recipe);
            }
        }
    }

    return matches.Distinct().ToArray();
}
public Recipe[]SearchRecipes(string[]searchTerms,Recipe[]recipes)
{
var matches=新列表();
foreach(配方中的var配方)
{
if(searchTerms.Contains(recipe.ChefName、StringComparer.OrdinalIgnoreCase))
{
火柴。添加(配方);
}
foreach(recipe.Hashtags中的var标记)
{
if(searchTerms.Contains(标记、StringComparer.OrdinalIgnoreCase))
{
火柴。添加(配方);
}
}
foreach(recipe.RecipeTle中的变量标题)
{
if(searchTerms.Contains(title,StringComparer.OrdinalIgnoreCase))
{
火柴。添加(配方);
}
}
}
返回匹配项.Distinct().ToArray();
}
但是,这将返回仅匹配一个或两个条件的项。例如,如果用户搜索“Chef Jane”和“疑难”,它也会返回来自“Chef Callum”的内容,因为“疑难”标签存在


如何确保返回的是完整的匹配项?

我不确定遍历该字符串中的成分的逻辑是什么,也许您也应该将其列为一个列表。无论如何,您可以为每个配方创建一个配方搜索词数组,并返回所有配方,其中所有用户输入搜索词都包含在这些配方搜索词中

public Recipe[] SearchRecipes(string[] searchTerms, Recipe[] recipes)
{
    var matches = new List<Recipe>();

    foreach (var recipe in recipes)
    {
        // Just flattening all the hash tags together with other recipe search terms
        var recipeTerms = recipe.HashTags
            .Concat(new string[]
            {
                recipe.RecipeTitle,
                recipe.ChefName,
                recipe.Ingredients
            });

        // Will include only the recipes where all search terms are matched in recipe terms
        if (searchTerms.All(searchTerm =>
                recipeTerms.Contains(searchTerm, StringComparer.OrdinalIgnoreCase))
        {
            matches.Add(recipe);
        }
    }

    return matches.ToArray();
}

请给我们输入数据和预期输出。如果您可以提供实际输出,它也会很有用。此逻辑不起作用,因为您过早地添加了匹配元素,通过匹配一个或两个属性,您需要做的是实现逻辑And,而当前代码是逻辑or。如果您使用Linq,它将是
All
,当前的是
Any
@MrinalKamboj对不起,您能解释一下什么是实现逻辑吗?或者我如何用Linq做到这一点?@Jordan1993很简单,你需要在匹配中添加一个配方,只有当你满足所有条件时,现在你甚至在满足一个条件时过早地添加元素,当你需要继续添加时,就像在满足一个条件时添加True一样,另一个条件为True,最后如果所有值均为True,则添加匹配项,否则为Truenot@Jordan1993你有没有试过我提供的解决方案?它做的正是你想要的,而且非常简单。
public Recipe[] SearchRecipes(string[] searchTerms, Recipe[] recipes)
    => recipes.Where(recipe =>
        {
            var recipeTerms = recipe.HashTags
                .Concat(new string[]
                {
                    recipe.RecipeTitle,
                    recipe.ChefName,
                    recipe.Ingredients
                });

            return searchTerms.All(searchTerm =>
                recipeTerms.Contains(searchTerm, StringComparer.OrdinalIgnoreCase));
        })
        .ToArray();