Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Left join #4

Open
hbulens opened this issue Sep 5, 2020 · 0 comments
Open

Left join #4

hbulens opened this issue Sep 5, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@hbulens
Copy link
Member

hbulens commented Sep 5, 2020

It would be nice to have a utility method for a left join that merges the lists using an expression. Instead of returning an enumerable of items that matches the predicate, it should also be possible to return only the first match.

The following pattern often occurs in our solution:

fields.GroupJoin<FieldDto, Caption, dynamic, FieldDto>(
  captions,
  x => new { x.FieldName, x.Context, x.SourceTable },
  y => new { y.FieldName, y.Context?.Name, y.SourceTable },
  (field, captions) =>
  {
    Caption caption = captions.FirstOrDefault();
    return caption == null ? field : field.With(y => y.FieldName = caption.DisplayName ?? y.FieldName);
  })

This is the current production code, which achieves the same thing:

return fields.Select(x =>
{
  Caption caption = captions.FirstOrDefault(captionFilter.FilterByAll(x.FieldName, x.Context, x.SourceTable));
  return caption == null ? x : x.With(y => y.FieldName = caption.DisplayName ?? y.FieldName);
});

This could be simplified by the utility method. In the end, it would look something like this:

return fields
 .LeftJoin(captions, (field, caption) => captionFilter.FilterByAll(field.FieldName, field.Context, field.SourceTable))
 .Select((field,caption) =>  field.With(y => y.FieldName = caption.DisplayName ?? y.FieldName));

Items without a match should proceed as usual so we can skip the null reference check.

@hbulens hbulens added the enhancement New feature or request label Sep 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant