Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Linq Query Criteria

Posted on 2014-09-19
11
Medium Priority
?
293 Views
Last Modified: 2016-02-15
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
0
Comment
Question by:Basicfarmer
  • 5
  • 4
  • 2
11 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40334106
Please elaborate.
0
 

Author Comment

by:Basicfarmer
ID: 40334109
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
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40334125
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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Basicfarmer
ID: 40334140
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)
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40334337
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.
0
 

Author Comment

by:Basicfarmer
ID: 40334656
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.
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 40334772
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
0
 

Author Comment

by:Basicfarmer
ID: 40334889
I will get to work on this and let you know where I end up.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40334898
Very good. Have a great day.
0
 

Author Closing Comment

by:Basicfarmer
ID: 40339653
Excellent response. Thank you...
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40339671
Not a problem, glad I was able to help.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question