【问题标题】:How to execute an ObjectQuery<T>如何执行 ObjectQuery<T>
【发布时间】:2011-04-07 12:35:59
【问题描述】:

Linq to Entities 使用实现 IQueryable 的 ObjectQuery。我通常使用 IQueryable 方法来过滤我的数据,但今天我需要创建一个特殊的 LIKE 语句。该框架一直认为它很聪明,并用波浪号“转义”了我的通配符“%”,这使得我的特殊 LIKE 语句无效。因此,在四处挖掘之后,我发现 ObjectQuery 上的 Where 方法有一个重载,它允许您将字符串与 ObjectParameters 一起传递。我已经这样做了,但它不像 IQueryable 那样执行。当我运行应用程序时,命中此代码后什么也没有发生。没有错误,也没有命中数据库,所以我知道我创建的查询实际上并没有执行。

如何处决这只小狗?

public IQueryable<tbl_Path> GetPathsByWildCardSearch(string searchTerm)
{
    return this.ObjectContext.tbl_Path
        .Where("FullPath NOT LIKE @p0 and FullPath LIKE @p1", 
            new ObjectParameter("p0", string.Format("%{0}%{1}%", searchTerm, tbl_Path.PathSeperator)),
            new ObjectParameter("p1", string.Format("%{0}%", searchTerm)));
}

【问题讨论】:

    标签: linq-to-entities sql-like tilde objectquery


    【解决方案1】:

    ;) 终于看到上面有一个 Execute() 方法。

    【讨论】:

      猜你喜欢
      • 2012-01-22
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      • 2010-09-29
      • 1970-01-01
      • 2014-08-16
      相关资源
      最近更新 更多