If you're a C# developer learning JavaScript—or a JavaScript developer curious about LINQ in C#—this guide is for you. Let's compare some of the most common data transformation methods between JavaScript Arrays and C# LINQ.
🔁 Overview Table
Purpose | JavaScript | C# LINQ |
---|---|---|
Filtering | array.filter() |
.Where() |
Projection (Select) | array.map() |
.Select() |
Condition Check |
array.some() / every()
|
.Any() / .All()
|
Aggregation | array.reduce() |
.Aggregate() |
Sorting | array.sort() |
.OrderBy() / .ThenBy()
|
Finding First | array.find() |
.First() / .FirstOrDefault()
|
Examples:
🍧 1. Filtering Items
C# LINQ
var adults = people.Where(p => p.Age >= 18);
JavaScript
const adults = people.filter(p => p.age >= 18);
🍧 2. Selecting/Projecting Fields
C# LINQ
var names = people.Select(p => p.Name);
JavaScript
const names = people.map(p => p.name);
🍧 3. Checking Conditions
C# LINQ
bool anyMinor = people.Any(p => p.Age < 18);
bool allAdults = people.All(p => p.Age >= 18);
JavaScript
const anyMinor = people.some(p => p.age < 18);
const allAdults = people.every(p => p.age >= 18);
🍧 4. Reducing/Aggregating Values
C# LINQ
int totalAge = people.Aggregate(0, (sum, p) => sum + p.Age);
JavaScript
const totalAge = people.reduce((sum, p) => sum + p.age, 0);
🍧 5. Sorting Items
C# LINQ
var sorted = people.OrderBy(p => p.Name).ThenBy(p => p.Age);
JavaScript
const sorted = people
.sort((a, b) => a.name.localeCompare(b.name) || a.age - b.age);
🍧 6. Finding the First Match
C# LINQ
var firstAdult = people.FirstOrDefault(p => p.Age >= 18);
JavaScript
const firstAdult = people.find(p => p.age >= 18);
🌟 Final Thoughts
JavaScript and C# may look different on the surface, but their array and LINQ methods are quite similar under the hood. If you're transitioning between the two, keep this guide handy to translate concepts quickly!
If you found this helpful, consider supporting my work at ☕ Buy Me a Coffee.