开发者

Similar features of LINQ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 9 years ago.

开发者_JAVA技巧 Improve this question

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.


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


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.


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]"
  }
}


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜