selectMany_<TCollection, TResult> method

IQueryable<TResult> selectMany_<TCollection, TResult>(
  1. Expr collectionSelector, {
  2. Expr? resultSelector,
})

Projects each element into a collection and flattens the result.

The collectionSelector is a single-parameter LambdaExpr whose body must return an Iterable (the inner collection for that outer element).

The optional resultSelector is a two-parameter LambdaExpr (outer, inner) that maps each pair to the final result type. When omitted, the inner element type is used.

Example:

final allItems = orders
.asQueryable
.selectMany_`<LineItem, LineItem>`(
Expr.lambda(
[Expr.param('o')],
Expr.member(Expr.param('o'), 'items'),
))
.toList;
// → flat List<LineItem> with every line item
//   from every order

Implementation

IQueryable<TResult> selectMany_<TCollection, TResult>(
  Expr collectionSelector, {
  Expr? resultSelector,
}) {
  if (collectionSelector is! LambdaExpr) {
    throw ArgumentError(
      'selectMany_ collectionSelector must be a LambdaExpr, '
      'got ${collectionSelector.runtimeType}',
    );
  }
  final cs = collectionSelector;
  if (cs.params.length != 1) {
    throw ArgumentError(
      'selectMany_ collectionSelector must take exactly 1 '
      'parameter, got ${cs.params.length}',
    );
  }
  LambdaExpr? rs;
  if (resultSelector != null) {
    if (resultSelector is! LambdaExpr) {
      throw ArgumentError(
        'selectMany_ resultSelector must be a LambdaExpr, '
        'got ${resultSelector.runtimeType}',
      );
    }
    rs = resultSelector;
    if (rs.params.length != 2) {
      throw ArgumentError(
        'selectMany_ resultSelector must take exactly 2 '
        'parameters (outer, inner), got ${rs.params.length}',
      );
    }
  }
  return _SelectManyEnumerableQuery<T, TCollection, TResult>(
    this,
    cs,
    rs,
  );
}