Maslow
Maslow

Reputation: 18746

Can I add a linq compiler to my project?

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?

Upvotes: 3

Views: 322

Answers (3)

AndyM
AndyM

Reputation: 1067

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

Upvotes: 0

JoshJordan
JoshJordan

Reputation: 12975

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.

Upvotes: 3

JaredPar
JaredPar

Reputation: 754505

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.

Upvotes: 2

Related Questions