tags:

views:

66

answers:

3

Hello Everyone -

I was playing around with LINQPad and was curious how I could implement similar behavior in my own app, namely: how can I allow the user to input a LINQ query against a known database context as a string and then run that query in the application?

For example, if I have the LINQ-to-SQL datacontext for the Northwind database in my application, I want the user to have the ability to type

from cust in Customers
where cust.City == "London"
select cust;

And I'll return the results of calling .ToList() on this query.

Any ideas/tips/links?

Thanks kindly

Mustafa

+1  A: 

You could see exactly how LINQPad does it by using .NET Reflector to disassemble the executable (LINQPad isn't open source). They actually even mention it in their license:

You are free to disassemble the executable to satisfy your curiosity.

Plus, this would be a great way to learn the inner workings of the tool, and find some neat tricks in their code.

Tim S. Van Haren
A: 

I suggest having a look into the source code of "Snippy", a nice tool from John Skeet's book "C# in depth". You can download it from the web site. The code file "Snippet.cs" has only about 130 lines of code and contains the relevant parts for compiling code on the fly.

Doc Brown
A: 

The System.CodeDom namespace might do what you're looking for. Check out this blog post:

http://blogs.msdn.com/lukeh/archive/2007/07/11/c-3-0-and-codedom.aspx

Though instead of public static void Main you could compile a static method that takes a DataContext class and returns IEnumerable using the provided LINQ query. Or whatever works.

Be mindful that everytime you compile code this way you're creating a new assembly, which would then need to be loaded into your application before you can execute it. Assemblies aren't garbage collected; if users are going to want to run many, many queries it could lead to a nasty memory leak.

And it'd also be a good idea to be mindful of possible attacks users can do by typing in whatever malicious code they want executed. But I don't have any rock solid advice for you there.

Tinister
Any idea offhand if this runs in Medium Trust? Just thinking ahead since I'm pretty sure I'll have to deploy to such an environment.Also, any clue how badly this affects performance vs. using Reflection in some way (which is how I vaguely thought I'd tackle this issue)
Mustafakidd