I am trying to write a dynamic where clause using LINQ to return all strings containing any keyword provided in an array of strings. The results do not return, as expected, with what I have so far, and looking at SQL, I see a problem.
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 be
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 '~'))
I hope someone can help me as I spent a couple of hours looking for a solution.
Thanks in advance
Rob c source share