PHP 中是否有与 C# 中的 LINQ 等效的东西?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5792388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:30:23  来源:igfitidea点击:

Is there something in PHP equivalent to LINQ in C#?

phplinqlanguage-comparisons

提问by eomeroff

Is there something in PHPequivalent to LINQin C#?

PHP 中是否有与 C# 中的LINQ等效的东西?

采纳答案by Justin Niessner

回答by cystbear

Try YaLinqo. It is the best LINQ for PHP.

试试雅林库。它是 PHP 的最佳 LINQ。

Here is a comparison table:

这是一个对比表:

Enter image description here

在此处输入图片说明

回答by Athari

(YaLinqo developer here.)

(这里是 YaLinqo 开发人员。)

Currently, there are three major implementations of LINQ in PHP:

目前,PHP 中 LINQ 的实现主要有以下三种:

  • YaLinqo— the most minimalistic library architecture-wise (4 classes), but the most featureful, the most performant, with the best documentation and the only one supporting "string lambdas".

  • Ginq— a library of average size (70 classes), on par with YaLinqo in the number of functions, around 1.5–3x times slower, contains custom collections, iterators and comparers, documentation contains argument types at best.

  • Pinq— a huge library (500 classes), supports parsing PHP and transforming into SQL and everything else, much slower than the rest, has a pretty website, but its documentation is average and functionality is lacking.

  • YaLinqo— 最简约的库架构(4 个类),但功能最强大、性能最高、文档最好,并且是唯一一个支持“字符串 lambdas”的库。

  • Ginq— 一个平均大小(70 个类)的库,在函数数量上与 YaLinqo 相当,大约慢 1.5-3 倍,包含自定义集合、迭代器和比较器,文档最多包含参数类型。

  • Pinq- 一个巨大的库(500 个类),支持解析 PHP 并转换为 SQL 和其他所有内容,比其他的慢得多,有一个漂亮的网站,但它的文档很一般,缺乏功能。

Other libraries aren't worth being mentioned (um, okay, LINQ for PHP, Phinq, PHPLinq and Plinq). They're barely tested, evaluations in them aren't lazy, they rely on weird concepts alien to both PHP and .NET developers. PHPLinqis the only one which actually supports databases, but it's more like DAL for generating SQL with a fixed call order rather than LINQ.

其他库不值得一提(嗯,好吧,LINQ for PHP、Phinq、PHPLinq 和 Plinq)。它们几乎没有经过测试,其中的评估并不懒惰,它们依赖于 PHP 和 .NET 开发人员都陌生的奇怪概念。PHPLinq是唯一真正支持数据库的,但它更像是 DAL,用于生成具有固定调用顺序的 SQL,而不是 LINQ。

If you ask me what library to choose, I'd just say to use YaLinqo if you need to work with arrays and objects and any ORM library (not LINQ) when you need to work with databases. But I'll try to explain why.

如果你问我选择什么库,我只会说如果你需要处理数组和对象以及任何 ORM 库(不是 LINQ),当你需要处理数据库时,我会说使用 YaLinqo。但我会尽力解释原因。

Performance

表现

YaLinqo is by far the fastest of the libraries. It's designed to be fast: it relies only on generators (which produce the fastest iterators); it uses only arrays (no custom collections implemented as wrappers around arrays). Its evolution is getting rid of slow and outdated features: removing custom collections, removing explicit iterators; and worsening code quality if it improves performance: choosing between multiple sort functions instead of using one generic solution, copy-pasting code to reduce the number of function calls.

YaLinqo 是迄今为止最快的库。它被设计得很快:它只依赖于生成器(生成最快的迭代器);它只使用数组(没有作为数组包装器实现的自定义集合)。它的演变正在摆脱缓慢和过时的功能:删除自定义集合,删除显式迭代器;如果提高性能,则代码质量会变差:在多个排序函数之间进行选择,而不是使用一种通用解决方案,复制粘贴代码以减少函数调用的次数。

Ginq and Pinq took another approach, they use explicit iterator classes. It bites a big chunk out of performance, but allows using iterators separately from fluent method syntax.

Ginq 和 Pinq 采取了另一种方法,它们使用显式迭代器类。它占用了很大一部分性能,但允许将迭代器与流畅的方法语法分开使用。

Furthermore, they both have performance traps. When you use property accessors syntax in Ginq, your code becomes 5 times slower. There're also surprises awaiting you when you try using arrays as keys. When you use joining in Pinq, your code becomes hundreds or thousands times slower (I'm not joking, see links below)Performace of joining in Pinq was fixed after my bug report.

此外,它们都有性能陷阱。当您在 Ginq 中使用属性访问器语法时,您的代码会变慢 5 倍。当您尝试使用数组作为键时,还有惊喜等着您。当您在 Pinq 中使用加入时,您的代码会变慢成百上千倍(我不是在开玩笑,请参阅下面的链接)在我报告错误后修复了加入 Pinq 的性能。

With YaLinqo, it's simpler: either it doesn't work (like arrays in keys), or it works with expected performance. Version 1 did have some hacks to make it possible, like in original LINQ, but the current version doesn't. It may require some adjustments: for example, instead of using an equality comparer, you'll need to produce keys which are equal in the same cases.

使用 YaLinqo,它更简单:要么不起作用(如键中的数组),要么以预期的性能运行。版本 1 确实有一些技巧可以使它成为可能,就像在原始 LINQ 中一样,但当前版本没有。它可能需要一些调整:例如,您需要生成在相同情况下相等的键,而不是使用相等比较器。

See articles: LINQ for PHP comparison: YaLinqo, Ginq, Pinqon CodeProject, LINQ for PHP: speed matterson Habrahabr (Russian). They cover YaLinqoPerf, git repository with performance tests comparing raw PHP, YaLinqo, Ginq and Pinq.

请参阅文章:用于 PHP 的 LINQ 比较:CodeProject 上的 YaLinqo、Ginq、Pinq用于 PHP 的 LINQ:Habrahabr(俄语)上的速度很重要。它们涵盖了YaLinqoPerf、git 存储库以及比较原始 PHP、YaLinqo、Ginq 和 Pinq 的性能测试。

Features

特征

The number of LINQ methods in YaLinqo and Ginq, as well as their functionaility, are pretty close. I'd say there's no clear winner, as both libraries provide methods the other one doesn't have. They are mostly extra methods unavailable in the original LINQ, so I wouldn't worry about it too much.

YaLinqo 和 Ginq 中 LINQ 方法的数量以及它们的功能非常接近。我想说没有明显的赢家,因为两个库都提供了另​​一个没有的方法。它们大多是原始 LINQ 中不可用的额外方法,所以我不会太担心。

Pinq looks like a deserted town. Methods are barebones and are often barely usable. While writing peformance tests, I often had to resort to custom more complex solutions for Pinq, while code for YaLinqo and Pinq usually differed only in method names (different naming conventions: "desc" vs. "descending" and things like that).

Pinq 看起来像一个荒凉的小镇。方法是准系统,通常几乎不可用。在编写性能测试时,我经常不得不为 Pinq 使用定制的更复杂的解决方案,而 YaLinqo 和 Pinq 的代码通常仅在方法名称上有所不同(不同的命名约定:“desc”与“descending”等等)。

On the other hand, Pinq is the only one which supports parsing PHP and generating SQL from it. Unfortunately, the only query provider is for MySQL and its state is a "demonstration". So, while Pinq has this unique feature, it can't be used yet, unfortunately.

另一方面,Pinq 是唯一一个支持解析 PHP 并从中生成 SQL 的。不幸的是,唯一的查询提供程序是针对 MySQL 的,它的状态是“演示”。所以,虽然 Pinq 有这个独特的功能,但不幸的是,它还不能使用。

If you want LINQ to databases to become a reality, I guess you have no choice but to start on working on a query provider for Pinq. It's a very complex task, and I doubt one developer is able to produce high-quality query providers for all databases alone.

如果您希望 LINQ to databases 成为现实,我想您别无选择,只能开始为 Pinq 开发查询提供程序。这是一项非常复杂的任务,我怀疑一个开发人员是否能够单独为所有数据库生成高质量的查询提供程序。

What Ginq has that YaLinqo doesn't is more advanced architecture. Where Setand Dictionaryclasses are used in Ginq, you'll see arrays and only arrays in YaLinqo. Where Comparerand EqualityComparerare used in Ginq, you'll see closures (or no equivalent) in YaLinqo. At the core, it's a design decision — whether library should use concepts natural for programmers in this language or for programmers used to the library in other languages. The libraries just made a choice.

Ginq 有而 YaLinqo 没有的是更先进的架构。当SetDictionary类在Ginq使用,你会看到在YaLinqo阵列和阵列只。凡ComparerEqualityComparer在Ginq使用,你会看到在YaLinqo关闭(或没有对应)。从本质上讲,这是一个设计决策——库应该使用对于这种语言的程序员还是习惯于其他语言的库的程序员来说自然的概念。图书馆只是做出了选择。

It should be noted that more complex architecture doesn't equal a good implementation. Ginq uses public function hash($v) { return sha1(serialize($v)); }for calculating key hashes in "sets", for example.

应该注意的是,更复杂的架构并不等于好的实现。例如,Ginqpublic function hash($v) { return sha1(serialize($v)); }用于计算“集合”中的键哈希。

Documentation

文档

YaLinqo stands out with a good reference documentation in PHPDoc and online (generated from PHPDoc). It's mostly documentation of LINQ in .NET from MSDN adapted to PHP. If you know what MSDN is, you know its quality.

YaLinqo 以 PHPDoc 和在线(从 PHPDoc 生成)中的良好参考文档脱颖而出。它主要是来自 MSDN 的 .NET 中 LINQ 的文档,适用于 PHP。如果您知道 MSDN 是什么,您就知道它的质量。

Ginq's documentation is almost non-existent, it usually contains just argument type hints.

Ginq 的文档几乎不存在,它通常只包含参数类型提示。

Pinq's documentation is relatively good (every major method has a sentence or two explaining what it does), but it's no match for YaLinqo's documentation.

Pinq 的文档比较好(每个主要方法都有一两句话解释它的作用),但它与 YaLinqo 的文档不匹配。

Both Ginq and Pinq have good introductory artcicles on the web which explain concepts to new developers. YaLinqo doesn't have any introductory documentation besides a crazy example in ReadMe which doesn't explain anything.

Ginq 和 Pinq 在网络上都有很好的介绍性文章,可以向新开发人员解释概念。YaLinqo 没有任何介绍性文档,除了自述文件中没有解释任何内容的疯狂示例。

Pinq has a pretty website too, the only one of the three libraries.

Pinq 也有一个漂亮的网站,这是三个库中唯一的一个。

Everything else

其他一切

All three libraries have good test coverage, Composer integration, permissive open-source licenses, and other properties of libararies which are ready to be used in production.

这三个库都具有良好的测试覆盖率、Composer 集成、宽松的开源许可证以及准备用于生产的库的其他属性。

For those who care about ancient PHP versions, YaLinqo 1.x requires PHP 5.3, YaLinqo 2.x requires PHP 5.5, Ginq requires PHP 5.3, Pinq requires PHP 5.4.

关心PHP老版本的朋友,YaLinqo 1.x需要PHP 5.3,YaLinqo 2.x需要PHP 5.5,Ginq需要PHP 5.3,Pinq需要PHP 5.4。

P.S. If you have any additions, or think I'm biased, please comment. Comments are what I miss after writing so much text. :)

PS 如果您有任何补充,或者认为我有偏见,请发表评论。评论是我写了这么多文字后想念的东西。:)

回答by Elliot Levin

A lot has changed in the PHP world over the last couple of years rendering most of the previous answers out of date.

在过去的几年里,PHP 世界发生了很多变化,使之前的大部分答案都过时了。

Here is a new comparison table of the main LINQ implementations for PHP:

这是 PHP 主要 LINQ 实现的新比较表:

LINQ Library Comparison Table

LINQ 库比较表

These libraries can all be installed via composer.

这些库都可以通过composer安装。

In summary I would recommend the PINQlibrary (I am biased as I am the author) because it is actively maintained, well documented and tested and provides an implementation of true LINQ in PHP.

总之,我会推荐PINQ库(因为我是作者,所以我有偏见),因为它得到了积极维护、良好的文档记录和测试,并在 PHP 中提供了真正的 LINQ 实现。

By trueLINQ I mean that the library is not only a fluent collection API for in-memory arrays but also implements real query parsing with expression trees. This allows the integration of this API with external data sources hence PHP Integrated Query. A demo of such functionality can viewed herewhere queries are being compiled into SQL and run against a MySQL database:

通过真正的LINQ,我的意思是该库不仅是用于内存数组的流畅集合 API,而且还使用表达式树实现了真正的查询解析。这允许与外部数据源此API的集成因此PHPtegrated Query。可以在此处查看此类功能的演示,其中查询被编译为 SQL 并针对 MySQL 数据库运行:

回答by nover

There is also phinqwhich has a more recent release that PHPLinq, and it seems to look more like LINQ to Objectsfor C# than PHPLinq does.

还有一个phinq,它有一个比 PHPLinq 更新的版本,它看起来更像是LINQ to Objectsfor C# 而不是 PHPLinq。

回答by Bastardo

Good PHP ORM Library?

好的 PHP ORM 库?

An answer to this question says

这个问题的答案说

Look into Doctrine.

Doctrine 1.2 implements Active Record. Doctrine 2+ is a DataMapper ORM.

Also, check out Xyster. It's based on the Data Mapper pattern.

Also, take a look at DataMapper vs. Active Record.

看道义。

Doctrine 1.2 实现了 Active Record。Doctrine 2+ 是一个 DataMapper ORM。

另外,请查看 Xyster。它基于数据映射器模式。

另外,看看 DataMapper 与 Active Record。

回答by moi_meme

See PHPLinq(a set of PHP classes mimicing C#3.0's LINQ).

请参阅PHPLinq(一组模仿 C#3.0 的 LINQ 的 PHP 类)。