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