Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4104986
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:11:18+00:00 2026-05-20T21:11:18+00:00

Hey, I want to write a query that the where in the query is

  • 0

Hey,
I want to write a query that the “where” in the query is a string something like”

Dim query as string= “Name =xxxx and Date > 10 ”

Dim t = from book in doc.Descendants(“books”) Select _
[Name] = book..value, [Date] = book..value….
Where (query)

I build the query string on run time

Thanks…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:11:18+00:00Added an answer on May 20, 2026 at 9:11 pm

    I’m not saying this is your case but I see this a lot from people that came from ASP classic where we used to build dynamic SQL strings all of the time. We tend to hope that LINQ will give us some more power in part of the code but let us use strings elsewhere. Unfortunately this isn’t true. Where takes a boolean argument and there’s no way around that. You can write your own parser that uses reflection and eventually returns a boolean but you’d be writing a lot of code that could be error prone. Here’s how you really should do it:

    Assuming this is our data class:

    Public Class TestObject
        Public Property Name As String
        Public Property Job As String
    End Class
    

    And here’s our test data:

        Dim Objects As New List(Of TestObject)
        Objects.Add(New TestObject() With {.Name = "A", .Job = "Baker"})
        Objects.Add(New TestObject() With {.Name = "B", .Job = "President"})
        Objects.Add(New TestObject() With {.Name = "C", .Job = "Bus Driver"})
        Objects.Add(New TestObject() With {.Name = "D", .Job = "Trainer"})
    

    What you want to do is create a variable that represents the data to search for:

        ''//This variable simulates our choice. Normally we would be parsing the querystring, form data, XML values, etc
        Dim RandNum = New Random().Next(0, 3)
        Dim LookForName As String = Nothing
        Select Case RandNum
            Case 0 : LookForName = "A"
            Case 1 : LookForName = "B"
            Case 2 : LookForName = "C"
        End Select
    
        ''//Query based on our name
        Dim Subset = (From O In Objects Select O Where (O.Name = LookForName)).ToList()
    

    If sometimes you need to search on Job and sometimes and sometimes you don’t you just might have to write a couple of queries:

        Dim Subset As List(Of TestObject)
        Select Case RandNum
            Case 0
                Subset = (From O In Objects Select O Where (O.Name = "A" And O.Job = "Baker")).ToList()
            Case Else
                Select Case RandNum
                    Case 1 : LookForName = "B"
                    Case 2 : LookForName = "C"
                End Select
                Subset = (From O In Objects Select O Where (O.Name = LookForName)).ToList()
        End Select
    

    And just to explain writing your own query parser (which is a path that I recommend you DO NOT go down), here is a very, very, very rough start. It only supports = and only strings and can break at multiple points.

    Public Shared Function QueryParser(ByVal obj As Object, ByVal ParamArray queries() As String) As Boolean
        ''//Sanity check
        If obj Is Nothing Then Throw New ArgumentNullException("obj")
        If (queries Is Nothing) OrElse (queries.Count = 0) Then Throw New ArgumentNullException("queries")
    
        ''//Array of property/value
        Dim NameValue() As String
        ''//Loop through each query
        For Each Q In queries
            ''//Remove whitespace around equals sign
            Q = System.Text.RegularExpressions.Regex.Replace(Q, "\s+=\s+", "=")
            ''//Break the query into two parts.
            ''//NOTE: this only supports the equal sign right now
            NameValue = Q.Split("="c)
            ''//NOTE: if either part of the query also contains an equal sign then this exception will be thrown
            If NameValue.Length <> 2 Then Throw New ArgumentException("Queries must be in the format X=Y")
    
            ''//Grab the property by name
            Dim P = obj.GetType().GetProperty(NameValue(0))
            ''//Make sure it exists
            If P Is Nothing Then Throw New ApplicationException(String.Format("Cannot find property {0}", NameValue(0)))
            ''//We only support strings right now
            If Not P.PropertyType Is GetType(String) Then Throw New ApplicationException("Only string property types are support")
    
            ''//Get the value of the property for the supplied object
            Dim V = P.GetValue(obj, Nothing)
            ''//Assumming null never equals null return false for a null value
            If V Is Nothing Then Return False
            ''//Compare the two strings, return false if something doesn't match.
            ''//You could use String.Compare here, too, but this will use the current Option Compare rules
            If V.ToString() <> NameValue(1) Then Return False
        Next
    
        ''//The above didn't fail so return true
        Return True
    End Function
    

    This code would allow you to write:

    Dim Subset = (From O In Objects Select O Where (QueryParser(O, "Name = A", "Job = Baker"))).ToList()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all i have code to write to an xml doc from asp string
hey, what do i have to write, if i want date&time which updates itself?
Hey I want to select records with name O'Neil, How can i do that
Hey, I'm trying to write a program that will accept new tasks from people,
Hey, I'm trying to write a program to convert from a BASE64 string to
Hey, I'm trying to write a program to convert from a BASE64 string to
hey i guys i want help from you. i am working on website project,
hey guys i want to fetch 3 tables in 1 single ado.net call from
hey I want to make a call from my application, so far I found
Hey, I need to write a program for OSX that will cause my macbook

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.