arogozine/LinqToTypeScript

functions GroupJoin, DefaultIfEmpty are not available

Closed this issue · 3 comments

I don't do this often but had to implement left join. This is not easy, a couple of shorthands from LINQ, GroupJoin and DefaultIfEmpty are not present. Can they be introduced? Thanks!


let list1: KV[] = [
  { key: 0, value: 'a' },
  { key: 1, value: 'b' },
  { key: 2, value: 'c' },
  { key: 3, value: 'd' },
];

let list2: KV[] = [
  { key: 0, value: 'aa' },
  { key: 1, value: 'bb' },
  { key: 3, value: 'dd' },
];


let rightGroups = from(list2)
  .groupBy(x => x.key)
  .toMap(x => x.key);

let groupLeftJoin = from(list1).select((left) => {
  let matches = rightGroups.get(left.key);
  return { left: left, right: matches ? from(matches) : from([null]) };
});

let leftJoin = groupLeftJoin.selectMany((x) =>
  x.right.select((right) => ({
    left: x.left,
    right,
  }))
);

I'll look into this once I get some time. You can also create a PR to add these APIs in.

Default If Empty will be part of the next release