views:

130

answers:

4

I'd like to allow some of our business analysts to write in linq instead of SQL where applicable. Corporate would never go for allowing linqpad to be installed, so where would I start on learning how to allow say simple linq queries or expressions in vs2008 project?

Is there sometype of eval() function for .net that would compile and run a linq expression?

+2  A: 

I'm assuming you mean a javascript style eval? Unfortunately no there is no eval style function in the BCL, C# or VB which would give the behavior you are looking for.

JaredPar
@Downvoter, care to explain? Sometimes the answer to the question is that "what you want to do is not possible in the way you want to do it"
JaredPar
Agreed - very bad form, especially without an explanation.
Marc Gravell
+3  A: 

Take a look at this tutorial on dynamically executing code. Its pretty complete, and you could simply restrict use to the Linq namespace and your DAL's, if you wanted. You could also add checks to make sure they're not (for example) performing more than one statement, as well as advanced data visualization.

JoshJordan
+1 with this I could store pieces of code in a SQL server, download them at run time.
Maslow
When you say advanced data visualization, I don't suppose you know how I could use the default debugger display for an object do you? Wether it's the mouse over one, or a watch window version.
Maslow
No, I'm not sure how to do that. I was thinking more along the lines of providing a PropertyEditor or a TreeView databound to the query result.
JoshJordan
A: 

Not directly, but you can build late-bound expressions and invoke them. The difference being you have to implement your own expression builder or DSL+parser. I only needed to support WHERE clauses so I implemented a Predicate object with XML serialization so I would have a human-readable persistence format. If you want something more SQL-like you can use Scott Gu's Dynamic LINQ Query Library which will parse LINQ-syntax predicates for key expression types (Where, OrderBy, Select, some others). If you don't need full LINQ syntax (ie you can separate each expression predicate) then you can use this directly. If you need a full LINQ syntax then you'll have to add some parsing, but it will really just be a matter of finding keyword-predicate pairs and executing them dynamically.

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

AndyM
+1  A: 

CheckOut http://linqcompiler.codeplex.com/

Mayukh