Convert SQL Query back to Linq Expression programmatically?

Everything is possible, it just requires a truckload of work. The problem is that you first have to parse the SQL query, and interpret the AST to convert it into a linq expression tree. This isn't trivial, as Linq isn't 1:1 compatible with sql: for example, in linq, aggregates on a groupby are external to the groupby, while in SQL they're internal: they have to be in the same scope as the groupby otherwise they won't work.

This kind of info is needed for converting linq queries into SQL queries but also the other way around So to store queries, I'd chose a different route, e.g. A specification pattern.

Everything is possible, it just requires a truckload of work. The problem is that you first have to parse the SQL query, and interpret the AST to convert it into a linq expression tree. This isn't trivial, as Linq isn't 1:1 compatible with sql: for example, in linq, aggregates on a groupby are external to the groupby, while in SQL they're internal: they have to be in the same scope as the groupby otherwise they won't work.

This kind of info is needed for converting linq queries into SQL queries but also the other way around. So to store queries, I'd chose a different route, e.g. A specification pattern.

I don't believe there's anything built into LINQ-to-SQL. In theory I dare say it would be possible (although it wouldn't guarantee roundtripping, as there are different ways of expressing the same query) - but very hard to do well.

Thing what can possibly help you - LINQPad. I suppose, it can't translate SQL to LINQ query, but it can translate LINQ to SQL. One option would be to track from SQL generated IL code.

For example: SELECT TOP (50) t0. Id, t0. Tralala FROM pamparam AS t0 Generates: IL_0001: ldarg.0 IL_0002: call LINQPad.User.

TypedDataContext. Get_pamparam IL_0007: ldc. I4.

S 32 IL_0009: call System.Linq.Queryable. Take IL_000E: call LINQPad.Extensions. Dump This way it's quite obvious, that LINQ query would look like: pamparam.

Take (50).

Oh d*mn... Question was - if it's possible to do this programmatically. :/ – Arnis L. May 21 '09 at 7:26.

The problem is that you first have to parse the SQL query, and interpret the AST to convert it into a linq expression tree. This isn't trivial, as Linq isn't 1:1 compatible with sql: for example, in linq, aggregates on a groupby are external to the groupby, while in SQL they're internal: they have to be in the same scope as the groupby otherwise they won't work. This kind of info is needed for converting linq queries into SQL queries but also the other way around.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions