15

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.

Kevin McKelvin
  • 3,467
  • 1
  • 27
  • 27

2 Answers2

20

You have some options:

  1. 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).

  2. 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

  3. Use Mono's compiler which emits MSIL directly (so no assembly per compilation and much faster) - Mono Compiler as a Service (MCS)

  4. 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

Community
  • 1
  • 1
Jeff
  • 35,755
  • 15
  • 108
  • 220
  • 1
    #1 is not practical. #3 doesn't work under the Microsoft CLR (tried it...). #4 works fine but yeah, it comes with limitations. – Roman Starkov Sep 23 '10 at 21:32
  • I'm going to try #3 out, I read a post a while ago on Miguel de Icaza's blog that Mono.CSharp now works on the MS CLR. – Kevin McKelvin Sep 23 '10 at 21:35
  • 2
    Number 3 does work under MS Windows CLR. Tried it and currently using it in production. If it's giving you prolems check my post on it. Kevin, if you go with number 3 make sure you follow the directions in my post I linked to or you won't be able to use LINQ and you'll end up with the same assembly per compile problem as CodeDom. – Jeff Sep 23 '10 at 22:58
  • I'm marking this as the answer, thanks. I'm battling with point #3 - actually getting the query to run and get the IQueryable back. If that doesn't work I'll fall back to point 4. – Kevin McKelvin Sep 24 '10 at 14:36
  • 1
    It took be a full day of battling too. but since then, it's worked well. This is our current production implementation (having already done 4, then 2, then 1 first). – Jeff Sep 24 '10 at 14:46
0

Going from a "magic string" to code objects always involves some sort of parsing. In this case, it might be best to work with the EditableExpression library (available free from Google Code). Take your string, and format it to look like the result of serializing a series of EditableExpressions. Then, simply deserialize it and convert to an expression tree.

KeithS
  • 70,210
  • 21
  • 112
  • 164