LinQ for TypeScript

3.2.0 · active · verified Tue Apr 21

linqts is a TypeScript library that ports LINQ (Language Integrated Query) capabilities, familiar to C# developers, to JavaScript and TypeScript environments. It provides a `List<T>` class that offers a rich set of methods for querying and manipulating collections in a fluent, declarative style, including operations like `Where`, `Select`, `Join`, `GroupBy`, `Min`, and `Max`. The library is currently stable at version 3.2.0 and maintains an active development cadence with major releases approximately annually and more frequent minor and patch updates addressing features and bug fixes. Its primary differentiator is the direct porting of the LINQ API surface, which makes it intuitive for developers coming from a .NET background, providing strong type safety through TypeScript.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic filtering, mapping, and joining operations using the `List` class to perform LinQ-style queries on in-memory collections.

import { List } from 'linqts';

interface Person { Name: string; Age: number; } 
interface Pet { Name: string; Owner: Person; Type: string; }

const people: Person[] = [
  { Name: 'Alice', Age: 30 },
  { Name: 'Bob', Age: 25 },
  { Name: 'Charlie', Age: 35 }
];

const pets: Pet[] = [
  { Name: 'Fido', Owner: people[0], Type: 'Dog' },
  { Name: 'Whiskers', Owner: people[1], Type: 'Cat' },
  { Name: 'Buddy', Owner: people[0], Type: 'Dog' }
];

// Example 1: Filtering and mapping
const youngAdultsNames = new List(people)
  .Where(p => p.Age < 30)
  .Select(p => p.Name)
  .ToArray();
console.log('Young Adults Names:', youngAdultsNames); // Expected: ['Bob']

// Example 2: Joining collections
const petsByOwner = new List(people).Join(
  pets,
  person => person,
  pet => pet.Owner,
  (person, pet) => ({ OwnerName: person.Name, PetName: pet.Name, PetType: pet.Type })
);
console.log('Pets by Owner:', petsByOwner.ToArray());
/* Expected: [
  { OwnerName: 'Alice', PetName: 'Fido', PetType: 'Dog' },
  { OwnerName: 'Bob', PetName: 'Whiskers', PetType: 'Cat' },
  { OwnerName: 'Alice', PetName: 'Buddy', PetType: 'Dog' }
]*/

view raw JSON →