I'm trying to do some really dynamic querying here - preferably without invoking the compiler at runtime though.
I have a string containing a LINQ expression, e.g.
var s = "from a in queryable where a.Type == 1 select a";
How can I get the resulting IQueryable or Expressions from that?
I've seen LINQPad and RavenDb both do this so I'm convinced there's a way, I just haven't found it yet.
The C function strtok() is a string tokenization function that takes two arguments: an initial string to be parsed and a const -qualified character delimiter. It returns a pointer to the first character of a token or to a null pointer if there is no token.
In Java, there exist three main approaches to parse a string: Parse string by using Java Split() method. Parse string by using Java Scanner class. Parse string by using StringUtils class.
In C, the strtok() function is used to split a string into a series of tokens based on a particular delimiter. A token is a substring extracted from the original string.
You have some options:
Do something homegrown, parsing the text and building an Expression Tree. The standard approach to this would be to use a language parser to parse the string (like ANTLR).
Use CodeDOM to compile the query (NOT recommended for a Production environent as this is slow and generates an assembly per compilation which will saturate your AppDomain with assemblies if you do many. Let me stress, don't go this route if you have any kind of volume - though this is what LINQPad does) - http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6a4defd2-76f0-4865-97b7-130e4ba7b50a
Use Mono's compiler which emits MSIL directly (so no assembly per compilation and much faster) - Mono Compiler as a Service (MCS)
Use Dynamic LINQ (has some limitations and restrictions, but basically does what is suggested in point #1 and is nice, lightweight, and has the ability to only allow certain method calls. It parses the text query and builds an Expression Tree from it) - http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With