arogozine / LinqToTypeScript

LINQ to TypeScript
https://arogozine.github.io/linqtotypescript/
MIT License
139 stars 18 forks source link

functions GroupJoin, DefaultIfEmpty are not available #18

Closed vgpro54321 closed 2 years ago

vgpro54321 commented 2 years ago

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,
  }))
);
arogozine commented 2 years ago

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

arogozine commented 2 years ago

Default If Empty will be part of the next release

arogozine commented 2 years ago

Added APIs in https://www.npmjs.com/package/linq-to-typescript/v/10.0.0-beta2