JavaScript 库的主要 LINQ 是什么?

我正在寻找一个 JavaScript 库,它允许我使用类似 LINQ 的语法来查询复杂的 JSON 对象。我快速搜索了一下,发现了几个有前途的选择,看起来他们可能会提供我所需要的:

从 LINQ 到 JavaScript 和 < a href = “ http://Hugoware.net/Projects/jLinq”rel = “ nofollow norefrer”> jLinq

  • 有没有人有经验 利用他们?
  • 有什么利弊?
  • 性能是否具有可比性?
  • 函数传递语法 LINQtoJavaScript 提供任何隐藏的好处 (我个人认为 JLinq 在一垒更有吸引力 扫一眼) ?
  • 你觉得缺少什么 在任何一个项目中?
  • 你试过联系作者吗? 他们的反应如何?
  • 什么项目使用更广泛?

我认为它将是第一个得到彻底试用的。

45631 次浏览

The most basic and frequently used Linq operators are very commonly defined in widely used JS libraries. They just have different names (in fact, they have more traditional names than in Linq). Select becomes map, Where becomes filter, First and FirstOrDefault become [0].

Almost no library I know of (including I think the ones you linked to) bother to make the implementation lazy as in .NET Linq, they just evaluate immediately using arrays.

For a very nice, complete set of functional list operations, try: http://osteele.com/sources/javascript/functional/

Have you seen Rx for Javascript, yet? That's what you want.

You might want to check out linq.js. It follows the .NET lambda syntax and looks to be well integrated in a Microsoft environment.

LINQ for JavaScript - http://linqjs.codeplex.com/

Pros

  • Implements all .NET 4.0 methods
  • Complete lazy evaluation
  • Full IntelliSense support for VisualStudio
  • Supports jQuery
  • Supports Windows Script Host
  • Binding for Reactive Extensions for JavaScript(RxJS) and IntelliSense Generator
  • NuGet install support
  • Updated recently (last release Jan 2011)
  • Syntax conforms to lambda syntax in C#

Cons

  • The linq.js library is a little large.
  • If you are already using jQuery or other js library, the most commonly used functionality is probably already available. See especially jQuery's filter, and 'Any' methods.

I recently made a LINQ library for JavaScript. It implemented most LINQ functions provided by .NET and it is the fastest of all the LINQ libraries.

http://fromjs.codeplex.com/

I recommend taking a look at underscore.js. It is not a direct LINQ port like some of the others, but is a very comfortable "LINQ-like" experience. It supports all the filter, sort and project options that I need and has excellent documentation and community support.

As a bonus for Knockout users, there is UnderscoreKO that adds Underscore's array methods to Knockout's observable arrays. Demo

$linq: http://jscriptlinq.codeplex.com/

var users = [{username: "asmith", domain: "north_america"},
{username: "tmcfarland", domain: "europe"},
{username: "cdeckard", domain: "nort_america"}];


var groups = [{user: "ASMITH", groupName: "base_users"},
{user: "TMCFARLAND", groupName: "admins"},
{user: "CDECKARD", groupName: "base_users"},
{user: "CDECKARD", groupName: "testers"}];


var results = $linq(users).join(groups,
function (x) { return x.username; },    // key for 'users'
"x => x.user",                          // key for 'groups'
function (outer, inner)                 // function to generate results
{
return "user: " + outer.username +
", domain: " + outer.domain +
", group: " + inner.groupName;
},
"(x, y) => x.toLowerCase() == y.toLowerCase()");    // compare keys case-insensitively

There are some duplicating libraries out there that try to port LINQ to JavaScript with a similar syntax and method names. However, in the JS community, the library that is getting really popular and providing the same functionality is Underscore.js.

I'm looking for something like this myself and came across...

http://www.hugoware.net/Projects/jLinq

This looks really great! Maybe I just don't understand the point of Rx and observables compared to setting event handlers through something like jQuery.

I've tried out most of these -- and I really like $linq: http://jscriptlinq.codeplex.com/ the best. It simply works the way you would expect c# linq to work -- including the chain ability.

I personally find the LINQ/set operations Union, Intersect, Except and Distinct on enumerables in .NET. very useful. There is a jquery plugin called jQuery Array Utilities which provides these methods to be used on arrays.

Code examples:

$.distinct([1, 2, 2, 3])

returns [1,2,3]

$.union([1, 2, 2, 3], [2, 3, 4, 5, 5])

returns [1,2,3,4,5]

$.instersect([1, 2, 2, 3], [2, 3, 4, 5, 5])

returns [2,3]

$.except([1, 2, 2, 3], [3, 4, 5, 5])

returns [1, 2]