我想寫一個動態where子句使用LINQ返回包含字符串數組中提供的任何關鍵字的所有行。結果不會如預期的那樣回到我目前爲止的狀態,並查看SQL我可以看到問題。建立一個動態的where子句的動態關鍵字或使用IQueryable C#Linq
IQueryable<comments> query = _db.comments;
if (score != null)
query = query.Where(x => x.score == score);
if (dateFrom != null)3
query = query.Where(x => x.date_created >= dateFrom);
if (dateTo != null)
query = query.Where(x => x.date_created <= dateTo);
if (keywords != null)
{
//how to use OR for each in array?
foreach (var keyword in keywords)
{
var keywordCondition = keyword;
query = query.Where(x => x.text.Contains(keywordCondition));
}
}
WHERE ([Extent1].[score] = @p__linq__0)
AND ([Extent1].[date_created] >= @p__linq__1)
AND ([Extent1].[date_created] <= @p__linq__2)
AND ([Extent1].[text] LIKE @p__linq__3 ESCAPE '~')
AND ([Extent1].[text] LIKE @p__linq__4 ESCAPE '~')
--should是
WHERE ([Extent1].[score] = @p__linq__0)
AND ([Extent1].[date_created] >= @p__linq__1)
AND ([Extent1].[date_created] <= @p__linq__2)
AND (([Extent1].[text] LIKE @p__linq__3 ESCAPE '~')
OR ([Extent1].[text] LIKE @p__linq__4 ESCAPE '~'))
我希望有人能幫助我,因爲我已經花了幾個小時,現在尋找一個解決方案。
在此先感謝
什麼問題,它是什麼意思:/ /如何使用OR每個數組? – MajkeloDev
您可能需要查看規範模式http://en.wikipedia.org/wiki/Specification_pattern。一個簡單的模式實現可以在這裏找到https://github.com/pmacn/Specific –