Level Up Coding

Coding tutorials and news. The developer homepage gitconnected.com && skilled.dev && levelup.dev

Follow publication

You're reading for free via Daniel Rusnok's Friend Link. Become a member to access the best of Medium.

Member-only story

LINQ vs TypeScript: Method Equivalents at a Glance

Daniel Rusnok
Level Up Coding
Published in
5 min read4 days ago

--

generated by ChatGPT

Not a member? Read for free here.

Introduction

After years of working as a .NET backend developer, I transitioned into frontend development with TypeScript and quickly realized something: While LINQ made working with collections in C# feel elegant and intuitive, doing the same in TypeScript wasn’t always straightforward. I often knew exactly what I wanted to do — filter, transform, group — but not how to do it in TypeScript.

This article is a practical guide for developers like me who are fluent in LINQ and want to quickly find the TypeScript equivalents of common LINQ methods. If you’re short on time, feel free to jump straight to the summary table at the end. But if you’d like to scroll through and pick up some extra tips and caveats along the way, you’re more than welcome.

This post is a follow-up to my previous article, where I shared my experience of moving from backend architecture to frontend development — what surprised me, what I missed, and what I had to relearn. This time, we’re diving straight into the code.

Select → map

C# (LINQ)

var names = people.Select(p => p.Name);
var doubled = new[] { 1, 2, 3 }.Select(n => n * 2);

TypeScript

const names = people.map(p => p.name);
const doubled = [1, 2, 3].map(n => n * 2);

Notes

  • Select works on any IEnumerable<T>, while map() only works on arrays.
  • In JavaScript, watch out for null or undefined—calling map() on them throws an error.
  • Both methods return a new collection with the same number of items.

Where → filter

C# (LINQ)

var active = users.Where(u => u.IsActive);

TypeScript

const active = users.filter(u => u.isActive);

Notes

  • Where and filter both return only items that match the condition.
  • filter() returns a new array with all matching elements.

First → find

C# (LINQ)

var user = users.First(u => u.IsActive);

TypeScript

const user = users.find(u => u.isActive);

Notes

  • First throws if no item is found; find() returns undefined.
  • In JS/TS, you need to handle the case when no item is found.

Any → some

C# (LINQ)

bool hasAdults = users.Any(u => u.Age >= 18);

TypeScript

const hasAdults = users.some(u => u.age >= 18);

Notes

  • Both return true if at least one item matches the condition.

All → every

C# (LINQ)

bool allAdults = users.All(u => u.Age >= 18);

TypeScript

const allAdults = users.every(u => u.age >= 18);

Notes

  • Both return true if all items match the condition.

OrderBy → sort() / toSorted()

C# (LINQ)

var sorted = numbers.OrderBy(n => n);

TypeScript

const sorted = [...numbers].sort((a, b) => a - b); // mutable version with copy
const sorted = numbers.toSorted((a, b) => a - b); // immutable (ES2023+)

Notes

  • sort() mutates the original array – use [...array] to avoid that.
  • toSorted() is a non-mutating alternative available in modern JS (ES2023).
  • For descending order: (a, b) => b - a.

SelectMany → flatMap

C# (LINQ)

var allTags = posts.SelectMany(p => p.Tags);

TypeScript

const allTags = posts.flatMap(p => p.tags);

Notes

  • SelectMany and flatMap both flatten one level of nested arrays.

Distinct → new Set() or custom filter

C# (LINQ)

var unique = numbers.Distinct();

TypeScript

const unique = [...new Set(numbers)];

Notes

  • Works well for primitives.
  • For objects, use filter + custom logic or helper libraries like lodash.uniqBy.

Aggregate → Reduce

C# (LINQ)

int sum = numbers.Aggregate((acc, n) => acc + n);

TypeScript

const sum = numbers.reduce((acc, n) => acc + n, 0);

Notes

  • Both are used to reduce a collection to a single value.
  • JS requires an initial value (e.g., 0) to avoid errors on empty arrays.

GroupBy → Object.groupBy() / custom logic

C# (LINQ)

var grouped = people.GroupBy(p => p.Country);

Typescript (modern JS)

const grouped = Object.groupBy(people, p => p.country);

TypeScript (fallback for older environments)

const grouped = people.reduce((acc, person) => {
(acc[person.country] ||= []).push(person);
return acc;
}, {} as Record<string, Person[]>);

Notes

  • Object.groupBy() is part of ES2024 and supported in recent environments.
  • It returns an object like { [key]: value[] }.
  • For older browsers or Node.js versions, use reduce() or libraries like lodash.groupBy.
  • For simpler code, consider lodash.groupBy.
import groupBy from 'lodash.groupby';
const grouped = groupBy(people, p => p.country);

Summary Table: LINQ vs TypeScript Methods

linq vs typescript methods cheatsheet

Libraries worth mentioning

JavaScript and TypeScript don’t have a built-in LINQ-style library, but several third-party libraries offer similar functionality and can make your code cleaner and more expressive:

  • LodashA very popular utility library with functions like map, filter, groupBy, uniqBy, and many more. Great for working with arrays and objects.
  • Ramda — A functional programming library focused on immutability and currying. Useful if you like a more declarative style.
  • linq.js — A direct port of LINQ to JavaScript. The syntax is almost identical to C# LINQ but is not as commonly used in modern TypeScript projects.

These libraries can help you write more concise code, especially for operations like grouping, distinct filtering on objects, or chaining complex transformations. However, for most basic scenarios, native Array.prototype methods like map, filter, reduce, or find are usually enough.

Honestly, I am using only Lodash. I have no experience with Ramda or Linq.js, and ChatGPT suggested those two as other possibilities.

Conclusion

If you’re a .NET developer transitioning into the frontend world, understanding how LINQ concepts map to TypeScript can save you a lot of time — and a lot of Google searches. While TypeScript doesn’t have a built-in LINQ library, many of the same operations are possible with native array methods or with the help of utility libraries like Lodash.

This article was created to help bridge that gap and serve as a quick reference when you know the LINQ way but can’t recall the TypeScript equivalent.

Useful Resources

From Backend to Frontend: My Journey

This article is part of my broader journey from .NET backend architect to frontend developer. If you’re curious about what surprised me, what I miss, and what I had to relearn, feel free to check out my previous post:
👉 From .NET Architect to Frontend Developer

Written by Daniel Rusnok

Full-stack dev with a frontend focus. React, TypeScript, .NET. Clean code enthusiast.

Write a response