JavaScript equivalents of C# LINQ
A practical translation guide for C# developers working with JavaScript arrays.
When you spend a lot of time writing C#, it’s natural to think in LINQ. Moving to JavaScript doesn’t mean abandoning those operations; the method names and a few of the rules are different.
Here’s a small translation guide for ordinary JavaScript arrays.
Filter, project and find
Start with a collection:
const products = [
{ name: 'Small synth', price: 80, inStock: true },
{ name: 'Tape echo', price: 150, inStock: false },
{ name: 'Drum machine', price: 120, inStock: true },
];
Where becomes filter. Select becomes map. You can chain them:
const availableNames = products
.filter(product => product.inStock)
.map(product => product.name);
// ['Small synth', 'Drum machine']
To find the first match, use find:
const affordable = products.find(product => product.price < 100);
// { name: 'Small synth', price: 80, inStock: true }
If there’s no match, find returns undefined. It doesn’t behave like C#’s First, which throws when no element matches.
A quick reference
| LINQ idea | JavaScript array operation |
|---|---|
Where(predicate) |
filter(predicate) |
Select(selector) |
map(selector) |
Any(predicate) |
some(predicate) |
All(predicate) |
every(predicate) |
FirstOrDefault(predicate) |
find(predicate), with undefined for no match |
Take(n) |
slice(0, n) |
Skip(n) |
slice(n) |
Count() |
length |
Count(predicate) |
filter(predicate).length |
Sum(selector) |
reduce((sum, item) => sum + selector(item), 0) |
These are useful correspondences, rather than identical contracts for every value or type.
Aggregate with an initial value
reduce can express the work you might use Aggregate or Sum for:
const totalPrice = products.reduce(
(total, product) => total + product.price,
0,
);
// 350
The initial 0 matters. It makes the result for an empty array well-defined and avoids treating the first product object as the accumulator.
Sort without changing the source
Unlike OrderBy, JavaScript’s sort changes the original array. Modern JavaScript offers toSorted when you want a sorted copy:
const byPrice = products.toSorted((a, b) => a.price - b.price);
// Small synth, Drum machine, Tape echo
// products remains in its original order
For environments without toSorted, copy first:
const byPrice = [...products].sort((a, b) => a.price - b.price);
Remember the execution model
Array filter and map run immediately and each produces a new array. They don’t have the deferred execution of many LINQ queries. That’s normally fine for a modest collection; it matters when you chain operations over very large data sets.
Use strict comparisons where appropriate, be explicit about missing results, and don’t assume JavaScript’s value equality matches C#’s.
For the full method contracts and browser compatibility, see the JavaScript Array reference on MDN.