LINQ to TypeScript

12.1.0 · active · verified Sun Apr 19

LinqToTypeScript is an active library, currently at stable version 12.1.0, that ports the Language Integrated Query (LINQ) functionality from .NET to TypeScript. It provides a comprehensive set of methods for querying, transforming, and manipulating collections, inspired by the IEnumerable, IAsyncEnumerable, and IParallelEnumerable interfaces. The library supports both synchronous and asynchronous operations, leveraging modern JavaScript features like `AsyncIterable` and `generators`. It targets TypeScript 5.6.X and ES2022, shipping exclusively as ECMAScript Modules (ESM). Key differentiators include its dual usage pattern (explicit `from` wrapper or extending native types via `initializeLinq`), support for parallel operations, and consistent release cadence with new LINQ methods being regularly added.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates both the `from` wrapper and the native type extension pattern using `initializeLinq` for synchronous and asynchronous LINQ operations on arrays.

import { from, initializeLinq, IEnumerable } from 'linq-to-typescript';

// 1. Declare that the JS types implement the IEnumerable interface
declare global {
    interface Array<T> extends IEnumerable<T> { }
    interface String extends IEnumerable<string> { }
}

// 2. Bind Linq Functions to Array, Map, etc
initializeLinq();

async function demonstrateLinq() {
  console.log('--- Using Wrapper (from) ---');
  const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  const evenNumbersWrapper = from(numbers)
    .where((x) => x % 2 === 0)
    .select((x) => x * 2)
    .toArray();
  console.log('Even numbers doubled (wrapper):', evenNumbersWrapper); // Expected: [4, 8, 12, 16, 20]

  console.log('\n--- Using Extended Native Types ---');
  const oddNumbersNative = numbers.where((x) => x % 2 !== 0).toArray();
  console.log('Odd numbers (native extension):', oddNumbersNative); // Expected: [1, 3, 5, 7, 9]

  console.log('\n--- Async Operations ---');
  const asyncData = [10, 20, 30];
  const processedAsyncData = await from(asyncData)
    .selectAsync(async (x) => {
      await new Promise(res => setTimeout(res, 50)); // Simulate async delay
      return x + 1;
    })
    .toArray();
  console.log('Processed async data:', processedAsyncData); // Expected: [11, 21, 31]
}

demonstrateLinq();

view raw JSON →