Linq Query Criteria

Experts, I need to have the criteria in my Linq query to be a variable. How can I do this?

query = From items In DataSet.Table("MyTable").AsEnumerable
               Where <MyStringVariable>
               Select Items
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Please elaborate.
BasicfarmerAuthor Commented:
Well, I need to build the criteria string from user selections from comboboxes and textboxes. So if I were using SQL I would make my criteria like this:

Dim strCriteria as String = " PartNumber='ABCD' AND Qty=25 AND Region='NorthEast'"

So I need to use this in my Linq query like this:

Dim Query = From items in DataSet.Table("MyTable")
        Where strCriteria
        Select items
käµfm³d 👽Commented:
Well it looks like you could use the Select method:

Dim Query = From items in DataSet.Table("MyTable").Select(strCriteria)
            Select items

Open in new window

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

BasicfarmerAuthor Commented:
I cant get it to work. I ran the application and the criteria string that is produce is actually:

Category=1 AND (Series=3 Or Series=0) AND (Head=2 OR Head=0) AND (Hand=1 OR Hand=0)
Fernando SotoRetiredCommented:
Hi Basicfarmer;

Seeming that you wish the where clause to be dynamic you will need to use something like the "Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library)". On this page it will show you how to use it and you will find two links on where you can download it, C# or VB .Net. Linq is a strongly type object and so you will need the Dynamic library to do the construction of the query. The other option would be to use the Expression class methods to build an expression tree and convert the results of the expression tree into a query, this option is much more difficult then the first option.
BasicfarmerAuthor Commented:
This is very overwhelming. I have downloaded everything from the site and reviewed. I seem to have the class DynamicQueryLibrary, but it talks about it being a DLL. Im not really sure what to do with all of this.
Fernando SotoRetiredCommented:
Hi Basicfarmer;

1. Create a New Visual Studio project using the Class Library as the template and call it something like DynamicLinqQuery.
2. Delete the Class1.vb file the project created.
3. In Solution Explorer select the project name.
4. Right click on it and select Add Existing Item.
5. Navigate to the file you downloaded into the DynamicQuery folder until you find the file Dynamic.vb and select it and click on the Add button to add it to your project.
6. The next step is strange but needs to be done on line 623 and 1875 remove the System.Globalization. from System.Globalization.CultureInfo.InvariantCulture and add the following Imports statement to the top of the file.
   The two lines should now look like this
    623  AddSymbol("@" & i.ToString(CultureInfo.InvariantCulture), value)
   1875  Return New ParseException(String.Format(CultureInfo.CurrentCulture, format, args), pos)
7. Put the project into release mode and build the application.
8. The dll should now be in the bin/release directory.

Now in the project you want to use this library do the following.
1. In Solution Explorer right click on the Reference node and select Add Reference. In the new Window click on Browse tab on the left and selecting the Browse button on the bottom right. In the dialog box navigate to the dll you just created and select it and click Add Button. Then click on the OK Button.

2. Add an Imports statement at the top of the file. Note that if when you created the dll you named it something other then DynamicLinqQuery then you will need to replace DynamicLinqQuery with what you called it.

Imports DynamicLinqQuery.System.Linq.Dynamic

That is it. Now you can use that dll where ever you need Linq dynamic queries.

If you have any issues let me know.

These are the Imports statism I used
Imports System.Collections.Generic
Imports System.Linq
Imports System.Linq.Expressions
Imports System.Runtime.CompilerServices
Imports System.Reflection
Imports System.Text
Imports System.Reflection.Emit
Imports System.Threading
Imports System.Globalization

Open in new window

These are the dll's needed in the DynamicLinqQuery dll you will create
DLL's needed in DynamicLinqQuery

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BasicfarmerAuthor Commented:
I will get to work on this and let you know where I end up.
Fernando SotoRetiredCommented:
Very good. Have a great day.
BasicfarmerAuthor Commented:
Excellent response. Thank you...
Fernando SotoRetiredCommented:
Not a problem, glad I was able to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.