【问题标题】:Using "in" operator in where clause of entity framework在实体框架的 where 子句中使用“in”运算符
【发布时间】:2012-06-08 13:19:05
【问题描述】:

我希望所有产品都在当前类别下可用,包括所有子类别。 我的代码是这样的:

int[] toCheck =new int[2];

toCheck[0] = 1;
toCheck[1] = 2;
toCheck[2] = 3;

var products = db.Products.Include(x => x.categoryByProductID).Where(x => x.CategoryID in ?);

这里categoryByProductID = 所有子类别, ? = 我如何使用 toCheck[] 数组

或使用预定义值检查CategoryID 的任何其他解决方案!!!

任何有用的帮助......

【问题讨论】:

    标签: asp.net-mvc-3 linq entity-framework razor


    【解决方案1】:

    您想稍微反过来看一下 - 类别列表是否包含该产品的类别?

    var products = db.Products.Include(x => x.categoryByProductID).Where(x => toCheck.Contains(x.CategoryID)); 
    

    【讨论】:

    【解决方案2】:
    var products = db.Products.Include(x => x.categoryByProductID).Where(x => toCheck.Any(y => y == x.CategoryID));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-10
      • 2011-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多