代码之家  ›  专栏  ›  技术社区  ›  Storm

如何编写LINQ查询来执行此操作

  •  0
  • Storm  · 技术社区  · 15 年前
    IQueryable<SomeType> result = (from x in Context.XXX select x);
    

    现在我需要做的是以下工作(编写伪代码,我需要实际代码):

    foreach(var i in items)
    {
      // Where the object "i" has 2 properties
      // 1) i.Operator (values of which can be AND or OR)
      // 2) i.SomeID (values of which can be 1 .. 10)
    
      // I need to build the LINQ query with "ands" and "ors" based on the i's in this foreach
    }
    
    2 回复  |  直到 15 年前
        1
  •  0
  •   Keith    15 年前

    我们使用动态LINQ库来完成您想要做的事情(在这里找到: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx )

    您可以使用如下内容:

    var finalResults = results.Where("Your where clause");
    

    其中“your where子句”是包含动态生成的where子句的字符串。

        2
  •  1
  •   Thomas Levesque    15 年前

    幼稚的方法是将调用链接到 Where 方法,但是这样你只能实现和行为

    退房 PredicateBuilder 约瑟夫·阿尔巴哈里的课。它允许使用OR运算符构建动态LINQ查询