Custom Query Language to LINQ?

c# expression-trees linq


I'm writing a tool that requires data from a source. This source will be user specified, and may be things like an SQL back-end, a proprietary database, flat file system, who knows.

I'd like my interface into fetching these types of query, to use Linq, as it seems the most C# friendly, and can leverage a lot of .NET code.

I've been doing some research, and have started by building an IQueryProvider through the great tutorial here. It's gotten me a large part of the way, but now I'm confused as to the best way of having the users convert the expression tree into their custom code.

I'm trying to figure out the best way to provide an easy interface for users to specify how to turn an expression tree into custom "code" (i.e., "SQL"), and it seems rather cumbersome and complex -- I imagine because it is exactly that.

My question is, what is the best way to go about converting an expression tree into a custom language?

Nearest I can tell is that I am to use the 'Context' class to do my custom parsing logic, but the API I use seems rather low level - are there any higher level operations I can do to simply map operations to strings?

4/24/2012 7:11:32 PM

Accepted Answer

There is no easy or straight forward way of converting expression tree in to your custom query language. You might want to give LinqExtender a try

Which implements a visitor pattern for converting between linq and your dsl.

LinqExtender is a toolkit for building custom LINQ providers. It provides an abstracted layer over the original IQyeryable and IQueryProvider implementation and provides a simplified syntax tree. Moreover, it covers things like projection , method calls , ordery by , member parsing, etc internally. Therefore developer can focus more on his main task minus the complexity


4/24/2012 7:13:49 PM

Popular Answer

In general, the best way to convert a Tree structure into some other form is to use the visitor pattern.

Specifically check out the ExpressionVisitor class on msdn.

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow