我有一个相当复杂的查询,我用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?
答案 0 :(得分: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
};