ASP.NETCore5最快的方式搜索类别和子类别
创始人
2024-09-17 12:30:23
0

要实现该功能,可以使用递归函数来搜索类别及其所有子类别。以下是示例代码:

public IActionResult SearchCategory(int categoryId, string searchString) { var allCategories = _context.Categories.ToList(); var categoryIds = GetCategoryIds(categoryId, allCategories);

var searchResults = _context.Products
                    .Include(p => p.Category)
                    .Where(p => categoryIds.Contains(p.CategoryId) && 
                                p.Name.Contains(searchString))
                    .ToList();

return View(searchResults);

}

private List GetCategoryIds(int categoryId, List allCategories) { var categoryIds = new List();

var category = allCategories.FirstOrDefault(c => c.Id == categoryId);
if (category != null)
{
    categoryIds.Add(category.Id);
    if (category.Children?.Count > 0)
    {
        foreach (var child in category.Children)
        {
            var childIds = GetCategoryIds(child.Id, allCategories);
            categoryIds.AddRange(childIds);
        }
    }
}

return categoryIds;

}

在这个示例中,我们首先获取所有的类别,然后使用递归函数GetCategoryIds来获取categoryId及其所有子类别的ID。之后,我们使用获取到的categoryIds来过滤搜索结果。

请注意,这个示例代码只是基础的示例,在实际中可能需要根据具体情况进行更改和优化。

相关内容

热门资讯

透视存在(WepoKe)透视辅... 透视存在(WepoKe)透视辅助工具(wepokeai代打的胜率)其实真的是有挂(详细透视插件教程)...
透视私人局(aapoker有猫... 透视私人局(aapoker有猫腻)wpk德州ai辅助神器(详细辅助解说技巧)其实真的是有挂是一款可以...
透视免费(WepokE)透明挂... 1、透视免费(WepokE)透明挂辅助器(wepoke辅助有挂)本来有挂(详细透视揭秘教程)。2、w...
透视有挂(wepoke辅助技巧... 透视有挂(wepoke辅助技巧)微扑克有假吗(详细辅助总结教程)先前真的有挂1)wepoke辅助技巧...
透视肯定(WepoKe)透视辅... 透视肯定(WepoKe)透视辅助app(wepoke插件)原来存在有挂(详细透视2025版教程)准备...
透视讲解(aapoker有外挂... 透视讲解(aapoker有外挂)aapoker发牌逻辑(详细辅助透明挂教程)本然真的是有挂;透视讲解...
透视好牌(wEPoke)透明挂... 透视好牌(wEPoke)透明挂辅助安装(wepower有外挂)都是真的有挂(详细透视科技教程)是一款...
透视ai代打(德州之星有外挂)... 透视ai代打(德州之星有外挂)德州ai人工智能(详细辅助教你攻略)起初真的有挂1)德州之星有外挂辅助...
透视了解(wePokE)透明挂... 透视了解(wePokE)透明挂辅助插件(wepoke辅助技巧)往昔是有挂(详细透视2025新版教程)...
透视教学(智星德州菠萝外挂)德... 透视教学(智星德州菠萝外挂)德扑之星怎么设置(详细辅助安装教程)其实是真的有挂;透视教学(智星德州菠...