首页 > 解决方案 > 如何使用实体框架中的字符串参数类型数组执行 where 查询?

问题描述

我在 asp.net 核心中有一个操作方法,其参数类型为字符串数组,其中包含多个类别名称

   public IActionResult Index(string[] categories,)
   {
       context.category.where(s=> s.name == string[] categories)
   }

我想在类别上下文中执行 where 查询,类别名称必须包含此数组中的所有值。不使用fororforeach循环

像这样:context.category.where(s=> s.name == string[] categories)

标签: c#asp.net-mvcasp.net-coreentity-framework-core

解决方案


可能是这样的:

context.Category
  .Where(contextCategory => categories.All(c => contextCategory.Contains(c))
  .ToList()

推荐阅读