I am working on my first project using LINQ (in mvc), so maybe something is very simple that I missed. However, the day of searching and experimenting showed nothing that works, hence the message.
I am trying to write a LINQ (Linq to SQL) query that will contain several conditions in a where statement, separated by OR or AND. We do not know how many conditions will be in the request before fulfillment. This is to manage the search filter, where the user can select several criteria for filtering.
select * from table
where table.col = 1
OR table.col = 2
OR table.col = 7
.... 'number of other conditions
Before I just build the SQL query as a string, by going through all the conditions. However, there seems to be a good way to do this in LINQ. I tried looking for expression trees, but at the moment they seem a little steep to me. Another idea was to execute a lambda function inside the where statement, for example:
For Each value In values
matchingRows = matchingRows.Where(Function(row) row.col = value)
However, this only works for conditions I. How to make ORs?
Pavel source
share