如何更改最终表达式中2个ESQL where子句的条件?

发布于 2024-09-08 02:20:36 字数 1058 浏览 3 评论 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,那是因为: 如何将 SQL 'LIKE' 与 LINQ to Entities 结合使用?

I have a fairly complex query where I am filtering results with a LIKE statement. Here is the query:

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));

And then:

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
    };

The problem is that in the final query, it generates an AND clause for the 2 LIKE filters:

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

How do I get that to be an OR clause?

Note: if you are wondering why I am using ESQL here, it is because of this: How to use SQL 'LIKE' with LINQ to Entities?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吾性傲以野 2024-09-15 02:20:36

尽管我的 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 
  };

我对那些不透明字符串中的“it”的用途感到困惑。码字的选择多么奇怪啊。


当然,如果你切换到 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
  };

Although my LinqToEntities experience is limited, my reading of the reference materials suggests this might work:

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 
  };

I'm baffled at what the "it" is for in those opaque strings. What a strange choice of codeword.


Of course, if you switch to LinqToSql, you can simply write this (but you already knew that):

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
  };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文