Skip to content
Yasser Moradi edited this page Aug 26, 2015 · 3 revisions

Description:

You should use GroupJoin, SelectMany and DefaultIfEmpty together to make Left Join possible.

Take a look at following sample: (Jerry Mawler has no book in booksData)

Sample:

let authorsData = [
  { authorId: 1, name: "John Smith" },
  { authorId: 2, name: "Harry Gold" },
  { authorId: 3, name: "Ronald Schwimmer" },
  { authorId: 4, name: "Jerry Mawler" }
];

let booksData =
  [
    { authorId: 1, title: "Little Blue Riding Hood" },
    { authorId: 3, title: "The Three Little Piggy Banks" },
    { authorId: 1, title: "Snow Black" },
    { authorId: 2, title: "My Rubber Duckie" },
    { authorId: 2, title: "He Who Doesn't Know His Name" },
    { authorId: 3, title: "Hanzel and Brittle" }
  ];

let leftJoinResults = authorsData.asEnumerable().groupJoin(booksData, author => author.authorId, book => book.authorId,
  (author, booksByAuthor) => ({ authorName: author.name, books: booksByAuthor }))
  .selectMany(authorBooks => authorBooks.books.asEnumerable().defaultIfEmpty(authorBooks.books, { title: 'None' }), (book, author) => ({ authorName: author.authorName, title: book.title }))
  .toArray();

Clone this wiki locally