0

我有一个相当复杂的查询,我在其中使用 LIKE 语句过滤结果。这是查询:

var qsFilter = entities.QueryStatements.Where("it.Statement LIKE @searchTerm", 
    new ObjectParameter("searchTerm", searchTerm));
var qtFilter = entities.QueryTables.Where("it.TableNames LIKE @searchTables", 
    new ObjectParameter("searchTables", searchTerm));

接着:

var tables = from t in entities.TraceLines
    join qs in qsFilter on t.QueryStatementHash equals qs.QueryStatementHash
    join qt in qtFilter on qs.QueryTableHash equals qt.QueryTableHash
    where t.CallTypeId == 64
    orderby t.Sequence
    select new
    {
        Name = qt.TableNames
    };

问题在于,在最终查询中,它为 2 个 LIKE 过滤器生成了一个 AND 子句:

WHERE ([Filter1].[Statement] LIKE @searchTerm) AND 
    ([Extent3].[TableNames] LIKE @searchTables)

我如何让它成为一个OR子句?

注意:如果您想知道我为什么在这里使用 ESQL,那是因为:How to use SQL 'LIKE' with LINQ to Entities?

4

1 回答 1

0

尽管我的 LinqToEntities 经验有限,但我对参考资料的阅读表明这可能有效:

var joins =
  from t in entities.TraceLines 
  join qs in entities.QueryStatements
    on t.QueryStatementHash equals qs.QueryStatementHash 
  join qt in entities.QueryTables
    on qs.QueryTableHash equals qt.QueryTableHash
  select new {t, qs, qt};

var filtered = joins
  .Where("it.Statement LIKE @searchTerm OR it.TableNames LIKE @searchTables"
    new ObjectParameter("searchTerm", searchTerm)
    new ObjectParameter("searchTables", searchTerm))
  .Where(x => x.t.CallTypeId == 64)

var orderedAndProjected =
  from x in filtered
  order by x.t.Sequence
  select new 
  { 
    Name = qt.TableNames 
  };

我对那些不透明字符串中的“它”是什么感到困惑。多么奇怪的代码字选择。


当然,如果你切换到 LinqToSql,你可以简单地这样写(但你已经知道了):

var query = 
  from t in myDC.TraceLines
  from qs in t.QueryStatements
  from qt in qs.QueryTables
  where t.CallTypeId == 64
  where SqlMethods.Like(qs.Statement, searchTerm)
     || SqlMethods.Like(qt.SearchTables, searchTerm)
  order by t.Sequence
  select new
  {
    Name = qt.TableNames
  };
于 2010-07-02T01:28:09.720 回答