We help IT Professionals succeed at work.
Get Started

Build LINQ query at runtime

mark32123 asked
Last Modified: 2012-08-14
I am trying to use LINQ to create custom filters.  My goal is to allow for the where clause of the LINQ query to change depending on what the user selects.

My main goal is to filter by selected names in a multi select listbox.

I am trying to do something along these lines

For Each itm In ListBox1.SelectedItems
            strg +=  itm.ToString & " Or "

Dim qry = (From invoice In tblCashIn.GetData
                   Where invoice.Customer_Name = strg
                  Select invoice.Customer_Name).ToList

What I am having trouble with is embedding the Or operator (or any operator, AND, XOR, AndAlso, etc...).  Since I am building the Or in as a string, the Where statement in the LINQ query does not pickup that it should be multiple parameters to search for.

Any help would be great.  I am not 100% sure this is possible with LINQ, if it is not, it would be great if you might have some comparable way of accomplishing my goal (Excluding basic SQL queries, I am trying to keep everything strongly typed if possible).

PS:  Is this what DLINQ is supposed to accomplish?
Watch Question
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE