2

Is there any feature similar to LINQ (.NET) in different languages like JAVA, PHP, etc?

kzh
  • 29
  • 4

7 Answers7

7

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.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1

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
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
1

Im really surprised that nobody has talked about it, but I've used a port of LINQ to PHP from Martin Balliauw, which works kinda nice. It works kinda well, you can do something like this:

$result = from('$employee')
    ->in( $employeeTable )
    ->where('$employee => strlen($employee->Name) < 5')
    ->orderBy('$employee->Name')
    ->select('$employee');

I even wrote a small blog post about it.

David Conde
  • 4,631
  • 2
  • 35
  • 48
1

This is valid Java, but needs this library: https://github.com/nicholas22/jpropel-light

import java.util.Arrays;
import lombok.ExtensionMethod;
import lombok.val;
import propel.core.utils.Linq;
import static propel.core.functional.predicates.Predicates.*;
import static propel.core.functional.projections.Projections.*;

@ExtensionMethod({Linq.class})
public class Main
{

  /**
   * @param args
   */
  public static void main(String[] args)
  {
    val names = new String[] { "john", "james", "john", "eddie" }.where(startsWith("j")).distinct();    
    System.out.println(Arrays.toString(names));

    // prints "[john, james]"
  }
}
NT_
  • 2,660
  • 23
  • 25
0

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
Valentin Golev
  • 9,965
  • 10
  • 60
  • 84
0

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.

therufa
  • 2,050
  • 2
  • 25
  • 39
  • LINQ is not part of ASP.NET, per se. It's part of the .NET Framework. ASP.NET is also part of the .NET Framework. The types in the .NET Framework can be used in any .NET language. Both VB.NET and C# are .NET languages that happen to have language syntax that makes the LINQ features of the .NET Framework easier to use. – John Saunders Jun 06 '10 at 19:03
0

http://code.google.com/p/phpreboot/ implements LINQ partially in a PHP-like language.

mario
  • 144,265
  • 20
  • 237
  • 291