Is there any feature similar to LINQ (.NET) in different languages like JAVA, PHP, etc?
There are many languages that have syntax or library functions for operating on sequences of objects in a mostly functional way. For example Python has lambda functions, list comprehensions, generators, and the itertools
module.
However I don't know of any language that can get anywhere near everything that LINQ can do as cleanly and concisely. Remember that LINQ is not just a way to operate on in-memory structures - LINQ has many providers that share a similar interface:
- LINQ to Objects
- LINQ to XML
- LINQ to SQL
- etc..
Microsoft have done a good job with LINQ. I am sure some other languages will take inspiration from the success of LINQ and consider how to add similar features in the near future.
While basic LINQ features, like Select
(map
) and Where
(filter
) are present in almost all contemporary programming languages, constructing SQL "on the fly" and lazily is a work for ORM
For example, Django ORM allow you to use such syntax for making a query:
Posts.objects.filter(user=peter).annotate(comment_count=Count('comment')).order_by('comment_count')
It's something like (not very sure) in LINQ:
from p in posts
where p.user == peter
select new { post = p, comment_count = comments.Where(c => c.post == p).Count() }
order by comment_count
Sure there are, but php is a programming language and not a framework like .net framework. So if you want to use features, you should download some libraries, orms or frameworks to get higher effectivity. I dont think that php has that powerful feature, that could be an equivalent of linq.
Scala has for-comprehensions that provide similar functionality as the LINQ.
Example: Find all attendees with name Fred that speak Danish.
C#:
var xs =
from att in attendees
where att.name == "Fred"
from lang in att.spokenLanguages
where lang == "Danish"
select att;
Scala:
val xs = for {
att <- attendees
if att.name == "Fred"
lang <- att.spokenLanguages
if lang == "Danish"
} yield att
http://code.google.com/p/phpreboot/ implements LINQ partially in a PHP-like language.