I'm trying to iterate for over an string array and dynamically create a IQueryable
query. Its pretty straightforward but here's where I'm stuck
var query = context.QuestionsMetaDatas.AsQueryable();
var keywords=new List<string>(){ "Test1","Test2" };
foreach(var key in keywords)
{
query=query.Where(a=>a.Text.Contains(key));
}
Now the problem is that when the query gets generated its compiles to
select * from QuestionsMetaDatas where Text Like "Test1" AND Text Like "Test2"
Instead of AND
I wanted the query to generate OR
...Now how can I achieve this?
I have used predicate builder like Raphael suggested, it's just one file to include in your project, then your example becomes:
Generating the OR query your are looking for.
Have you tried contains the other way?
this is like an
IN
clauseyou could look at predicate builder, or build your own expression (here a possble solution with a static extension method on
IQueryable<QuestionsMetadatas>
)where
lambda
will look like that :and usage is
or shorter