如何在实体框架中执行带有字符串参数类型数组的查询?

时间:2020-06-06 20:11:19

标签: c# asp.net-mvc asp.net-core entity-framework-core

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

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

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

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

2 个答案:

答案 0 :(得分:1)

类似这样的东西:

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

答案 1 :(得分:0)

使用Any是按类别名称对其进行过滤的最佳方法:

Context.Category.AsEnumerable().Where(c => categories.Any(s => c.LatinName == s)).ToList();