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…
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.
Wheretakes 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:
And here’s our test data:
What you want to do is create a variable that represents the data to search for:
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:
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.This code would allow you to write: