Dynamically building LINQ query using OR operator in VB
Question
I need to build a dynamic linq query with or operators. I have seen PredicateBuilder but that is in C# and my project is in VB. Basically I need to build a WHERE clause similar to this:
Where((this = 1 AND that = 2) OR (this = 1 AND that = 4) OR (this = 2 AND that = 4))
but the problem is the number will have to be determined dynamically at runtime, and added using a loop, like
for each item in myItems
query = query.OR (this = item.a AND this = item.b)
next
How could I go about doing that?
Solution
Got the LinqKit.dll working, now using PredicateBuilder even with my VB project and it works very well!
For anyone else needing this see http://rocksthoughts.com/blog/archive/2008/04/10/linq-to-sql-dynamic-queries-3-ands--ors-together.aspx, very good article on how to do this.
OTHER TIPS
Have you looked into the LINQ Dynamic Query Library?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow