您可以使用 Python 或 Boo 等语言执行类似 LINQ 的查询吗?
采用这个简单的 C# LINQ 查询,并假设 db.Numbers
是一个包含一列 Number
的 SQL 表:
var result =
from n in db.Numbers
where n.Number < 5
select n.Number;
这在C#中运行得非常高效,因为它会生成一个SQL查询,类似于
select Number from Numbers where Number < 5
What it doesn't do is select all< /em> 数据库中的数字,然后在 C# 中过滤它们,就像一开始看起来的那样。
Python 支持类似的语法:
result = [n.Number for n in Numbers if n.Number < 5]
但是这里的 if 子句在客户端而不是服务器端进行过滤,效率低得多。
Python 中是否有像 LINQ 一样高效的东西? (我目前正在评估Python与IronPython与Boo,因此适用于任何这些语言的答案都很好。)
Take this simple C# LINQ query, and imagine that db.Numbers
is an SQL table with one column Number
:
var result =
from n in db.Numbers
where n.Number < 5
select n.Number;
This will run very efficiently in C#, because it generates an SQL query something like
select Number from Numbers where Number < 5
What it doesn't do is select all the numbers from the database, and then filter them in C#, as it might appear to do at first.
Python supports a similar syntax:
result = [n.Number for n in Numbers if n.Number < 5]
But it the if
clause here does the filtering on the client side, rather than the server side, which is much less efficient.
Is there something as efficient as LINQ in Python? (I'm currently evaluating Python vs. IronPython vs. Boo, so an answer that works in any of those languages is fine.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
sqlalchemy 中的 sqlsoup 为您提供了 python 中最快的解决方案,我认为如果您想要一个清晰的(ish ) 一班轮。 看页面就可以看到。
它应该是这样的...
sqlsoup in sqlalchemy gives you the quickest solution in python I think if you want a clear(ish) one liner . Look at the page to see.
It should be something like...
LINQ 是 C# 和 VB.NET 的语言功能。 它是编译器识别并特殊处理的特殊语法。 它还依赖于另一种称为表达式树的语言功能。
表达式树有一点不同,因为它们不是特殊的语法。 它们的编写方式与任何其他类实例化一样,但编译器确实通过将 lambda 转换为运行时的实例化来特殊对待它们 抽象语法树。 可以在运行时操作它们以生成另一种语言(即 SQL)的命令。
C# 和 VB.NET 编译器采用 LINQ 语法,并将其转换为 lambda,然后将它们传递到表达式树实例化中。 然后有一堆框架类来操纵这些树来生成 SQL。 您还可以找到其他提供“LINQ 提供程序”的库(包括 MS 生成的库和第三方库),这些库基本上弹出一个不同的 AST 处理器,以从 LINQ 生成除 SQL 之外的内容。
因此,用另一种语言做这些事情的一个障碍是它们是否支持运行时 AST 构建/操作。 我不知道 Python 或 Boo 的实现是否有这种功能,但我还没有听说过任何这样的功能。
LINQ is a language feature of C# and VB.NET. It is a special syntax recognized by the compiler and treated specially. It is also dependent on another language feature called expression trees.
Expression trees are a little different in that they are not special syntax. They are written just like any other class instantiation, but the compiler does treat them specially under the covers by turning a lambda into an instantiation of a run-time abstract syntax tree. These can be manipulated at run-time to produce a command in another language (i.e. SQL).
The C# and VB.NET compilers take LINQ syntax, and turn it into lambdas, then pass those into expression tree instantiations. Then there are a bunch of framework classes that manipulate these trees to produce SQL. You can also find other libraries, both MS-produced and third party, that offer "LINQ providers", which basically pop a different AST processer in to produce something from the LINQ other than SQL.
So one obstacle to doing these things in another language is the question whether they support run-time AST building/manipulation. I don't know whether any implementations of Python or Boo do, but I haven't heard of any such features.
仔细查看 SQLAlchemy。 这可能可以满足您的大部分需求。 它为您提供了在服务器上运行的普通 SQL 的 Python 语法。
Look closely at SQLAlchemy. This can probably do much of what you want. It gives you Python syntax for plain-old SQL that runs on the server.
我相信当 IronPython 2.0 完成时,它将提供 LINQ 支持(请参阅此线程 一些示例讨论)。 现在你应该能够写出类似这样的东西:
IronPython 2.0b4 中可能有更好的东西 - 有很多 当前关于如何处理命名冲突的讨论。
I believe that when IronPython 2.0 is complete, it will have LINQ support (see this thread for some example discussion). Right now you should be able to write something like:
Something better might have made it into IronPython 2.0b4 - there's a lot of current discussion about how naming conflicts were handled.
Boo 支持使用与 python 相同语法的列表生成器表达式。 有关详细信息,请查看有关 生成器表达式 和 列表推导式。
Boo supports list generator expressions using the same syntax as python. For more information on that, check out the Boo documentation on Generator expressions and List comprehensions.
LINQ 的一个关键因素是编译器生成表达式树的能力。
我在 Nemerle 中使用一个宏,将给定的 Nemerle 表达式转换为表达式树对象。
然后我可以将其传递给 IQueryables 上的Where/Select/etc 扩展方法。
它不太符合 C# 和 VB 的语法,但对我来说已经足够接近了。
我通过这篇文章的链接获得了 Nemerle 宏:
http://groups.google.com/group/nemerle-dev/ browser_thread/thread/99b9dcfe204a578e
应该可以为 Boo 创建类似的宏。 然而,鉴于您需要支持大量可能的表达式,这是一项相当大的工作。
Ayende 在这里给出了概念证明:
http://ayende.com/Blog/archive/2008/ 08/05/Ugly-Linq.aspx
A key factor for LINQ is the ability of the compiler to generate expression trees.
I am using a macro in Nemerle that converts a given Nemerle expression into an Expression tree object.
I can then pass this to the Where/Select/etc extension methods on IQueryables.
It's not quite the syntax of C# and VB, but it's close enough for me.
I got the Nemerle macro via a link on this post:
http://groups.google.com/group/nemerle-dev/browse_thread/thread/99b9dcfe204a578e
It should be possible to create a similar macro for Boo. It's quite a bit of work however, given the large set of possible expressions you need to support.
Ayende has given a proof of concept here:
http://ayende.com/Blog/archive/2008/08/05/Ugly-Linq.aspx