LINQ-like syntax using tagged templates.
npm install iterable-query-linqLINQ-like syntax using tagged templates.
npm install iterable-query-linq
`
Usage
`ts
import { linq } from "iterable-query-linq";
const q = linq
;
`
Syntax
A query must begin with a from clause, may consist of zero or more other clauses, and must end with either a group clause or a select clause:
`ts
linq
linq
`
$3
- from
- join
- join into
- let
- where
- orderby
- group
- group into
- select
- select into
from Clause
The from clause is used to bind an identifier to each element in an expression:
#### Basic usage
`
from ID of SOURCE
`
$3
Multiple from clauses can be chained together to form a cartesian join, resulting
in the cartesian product of the elements in each sequence.
$3
If _source_ is an async iterable, you must indicate async iteration using the await modifier. Async iteration is only supported in a linq.async block.
#### Example
`ts
linq.async
`
join Clause
The join clause is used to define a one-to-one relationship between the elements two iterables. All joins
are performed using an "equijoin" comparing the strict equality of the keys
selected from the outer and inner iterables:
#### Basic usage
`
join INNER_ID of INNER_SOURCE on OUTER_KEY equals INNER_KEY
`
join into Clause
The join into clause is similar to the join clause except that it creates a one-to-many relationship in the form of a group join:
#### Basic usage
`
join INNER_ID of INNER_SOURCE on OUTER_KEY equals INNER_KEY into ID
`
let Clause
The let clause creates a variable binding that persists through the query body
and is used to capture per-element state:
#### Usage
`
let ID = EXPRESSION
`
#### Examples
`ts
linq
`
where Clause
The where clause filters the iterable, skipping items that do not match the supplied criteria:
#### Usage
`
where EXPRESSION
`
#### Examples
`ts
linq
`
orderby Clause
The orderby Clause is used to sort an iterable using one or more comparators:
#### Usage
`
orderby EXPRESSION [ascending|descending] [, ...]
`
#### Examples
`ts
linq
`
group Clause
The group clause terminates a query and is used to group items with the same key:
#### Usage
`
group ELEMENT by KEY
`
#### Examples
`ts
linq
`
group into Clause
The group into clause is similar to the group clause, except that it introduces
a new binding that can be used in a subsequent query body:
#### Usage
`
group ELEMENT by KEY into ID
`
#### Examples
`ts
linq
`
select Clause
The select clause terminates a query and is used to select the resulting element
for each element in the source:
#### Usage
`
select EXPRESSION
`
#### Examples
`ts
linq
`
select into Clause
The select into clause is similar to the select clause, except that it introduces
a new binding that can be used in a subsequent query body:
#### Usage
`
select EXPRESSION into ID
`
#### Examples
`ts
linq
`
API
You can run queries using the built-in API as well (requires NodeJS):
`ts
import { parseAndExecuteQuery, parseAndExecuteQueryAsync } from "iterable-query-linq";
...
const users = ...;
const q = parseAndExecuteQuery(
, { users });
...
const asyncUsers = ...;
const q = parseAndExecuteQueryAsync(
, { users: asyncUsers });
`
REPL
You can also start a NodeJS REPL that evaluates queries:
`ts
import { startLinqRepl } from "iterable-query-linq";
const users = ...;
startLinqRepl({ users });
// > from user of users
// ... select user.name
``