Is it possible to somehow programmatically convert a sql query to a linq expression tree? The sql query is supposed to be generated by this linq query, so I'm thinking of it as a poor man's serialization\deserialization of linq queries to t-sql format.
是否有可能以某种方式以编程方式将sql查询转换为linq表达式树? sql查询应该由这个linq查询生成,所以我认为它是一个穷人的linq查询到t-sql格式的序列化\反序列化。
Thank you.
3 个解决方案
#1
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.
一切皆有可能,它只需要大量的工作。问题是您首先必须解析SQL查询,并解释AST以将其转换为linq表达式树。这并非易事,因为Linq与sql不是1:1兼容:例如,在linq中,groupby上的聚合在groupby外部,而在SQL中它们是内部的:它们必须在同一范围内作为团体,否则他们将无法工作。将linq查询转换为SQL查询需要这种信息,但反过来也需要这种信息。
So to store queries, I'd chose a different route, e.g. a specification pattern
所以为了存储查询,我选择了不同的路由,例如规范模式
#2
I don't believe there's anything built into LINQ-to-SQL.
我不相信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.
从理论上讲,我敢说它是可能的(虽然它不能保证往返,因为有不同的表达相同查询的方式) - 但很难做得很好。
#3
Thing what can possibly help you - LINQPad. I suppose, it can't translate SQL to LINQ query, but it can translate LINQ to SQL.
这可能对你有所帮助 - LINQPad。我想,它不能将SQL转换为LINQ查询,但它可以将LINQ转换为SQL。
One option would be to track from SQL generated IL code. For example:
一种选择是跟踪SQL生成的IL代码。例如:
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:
这种方式很明显,LINQ查询看起来像:
pamparam.Take (50)
#1
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.
一切皆有可能,它只需要大量的工作。问题是您首先必须解析SQL查询,并解释AST以将其转换为linq表达式树。这并非易事,因为Linq与sql不是1:1兼容:例如,在linq中,groupby上的聚合在groupby外部,而在SQL中它们是内部的:它们必须在同一范围内作为团体,否则他们将无法工作。将linq查询转换为SQL查询需要这种信息,但反过来也需要这种信息。
So to store queries, I'd chose a different route, e.g. a specification pattern
所以为了存储查询,我选择了不同的路由,例如规范模式
#2
I don't believe there's anything built into LINQ-to-SQL.
我不相信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.
从理论上讲,我敢说它是可能的(虽然它不能保证往返,因为有不同的表达相同查询的方式) - 但很难做得很好。
#3
Thing what can possibly help you - LINQPad. I suppose, it can't translate SQL to LINQ query, but it can translate LINQ to SQL.
这可能对你有所帮助 - LINQPad。我想,它不能将SQL转换为LINQ查询,但它可以将LINQ转换为SQL。
One option would be to track from SQL generated IL code. For example:
一种选择是跟踪SQL生成的IL代码。例如:
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:
这种方式很明显,LINQ查询看起来像:
pamparam.Take (50)