Solved

Linq Query Criteria

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

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 62

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 62

Accepted Solution

by:
Fernando Soto earned 500 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 62

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 62

Expert Comment

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

863 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

25 Experts available now in Live!

Get 1:1 Help Now