【问题标题】:Composing a where clause in LINQ query at runtime在运行时在 LINQ 查询中编写 where 子句
【发布时间】:2010-09-10 20:34:14
【问题描述】:

我得到一个字符串数组,我想查看域对象中的特定数量的数据字段是否包含所有这些字符串。我知道编译时的数据字段,但我不知道编译时数组的大小。

有没有一种方法可以在运行时编写 where 子句,以便我可以在单个 linq 查询中执行我想要的操作。

如果您想知道为什么它是单个查询:我想尽可能减少到数据库的往返次数。

public IEnumerable<domainObjects> GetObjectsWith(string[] data)
{
    var results = from d in domainObjects
                  where 
                  (d.Data.Contains(data[0]) && d.Data.Contains(data[1]) && ...)
                  ||
                  (d.Data2.Contains(data[0]) && d.Data.Contains(data[1]) && ...)
                  .
                  .
                  . // Data fields known at compile-time
}

最终结果是,给定 2 个对象:

domainObject  { Address = "1st st", Description="has couch and bed" }
domainObject2 { Address = "1st rd", Description="has couch" }

{ "couch", "bed" } 的查询只会返回域对象 1,但{ "couch" } 的查询会返回两者。

类似{ "1st", "couch", "bed" } 的查询也会返回两者。

【问题讨论】:

  • 在设计时是否知道域对象的属性?如果不是反射可能是你最好的选择。如果是这样,您可以从域对象中公开一个 IEnumerable 可搜索字段。
  • 关于“单个查询”:您可以在多个 C# 语句中组合查询,并且在执行(数据库读取)方面仍然是单个查询。阅读“延迟执行”以了解原因。

标签: c# linq where-clause


【解决方案1】:

您应该使用 PredicateBuilder,它是一个免费的实用程序类,允许您在运行时使用 And's 和 Or's 构造复杂的 where 子句。您可以循环遍历您的数组并以这种方式构建您的 where 子句。

http://www.albahari.com/nutshell/predicatebuilder.aspx

【讨论】:

    【解决方案2】:

    有 3 种主要方法可以做到这一点,PredicateBuilder、Dynamic Linq 库或操作您自己的表达式树(前两种可以帮助您在幕后完成)。

    如果您提前了解所有属性,PredicateBuilder 是您的最佳选择。如果它们是动态的(即用户选择它们,那么 Dynamic Linq 是最好的选择)。

    另见Is there a pattern using Linq to dynamically create a filter?

    link text

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      • 2018-08-14
      • 2020-12-31
      • 2019-05-07
      • 2019-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多