Solved

Linq Query Criteria

Posted on 2014-09-19
11
270 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Please elaborate.
0
 

Author Comment

by:Basicfarmer
Comment Utility
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 74

Expert Comment

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

Author Comment

by:Basicfarmer
Comment Utility
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 62

Expert Comment

by:Fernando Soto
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Basicfarmer
Comment Utility
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 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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
Comment Utility
I will get to work on this and let you know where I end up.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Very good. Have a great day.
0
 

Author Closing Comment

by:Basicfarmer
Comment Utility
Excellent response. Thank you...
0
 
LVL 62

Expert Comment

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article will show, step by step, how to integrate R code into a R Sweave document
This video teaches viewers about errors in exception handling.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now