Skip to content

Linq.Join with key as parameter #28733

Closed
Closed

Description

@Thaina

Sometimes the Join would have a key being calculated. So it would be useful to have an overload of Join passing the matching key along with the value selector

public IEnumerable<V> Join<T0,T1,K,V>(this IEnumerable<T0> source,IEnumerable<T0> target,Func<T0,K> key0Selector,Func<T1,K> key1Selector,Func<V,K,T0,T1> valueSelector);

////

var results = items0.Join(items1,(outer) => ComplexLogicForKey0(outer),(inner) => ComplexLogicForKey1(inner),(key,outer,inner) => (key,outer,inner));

Maybe the tuple version of Join should also use this as return value instead

/// overload of Join
public IEnumerable<(K,T0,T1)> Join<T0,T1,K>(this IEnumerable<T0> source,IEnumerable<T0> target,Func<T0,K> key0Selector,Func<T1,K> key1Selector);

Activity

  1. Clockwork-Muse commented on Feb 20, 2019

    @Clockwork-Muse
    Contributor

    Curious question:
    If the derivation logic for a key is so expensive you want to pass it along with the result, would your program perform better/more consistently if you calculated/stored it in the original object? Especially since it has to iterate through all the inner elements before it can return even the first result.

  2. Thaina commented on Feb 20, 2019

    @Thaina
    Author

    @Clockwork-Muse Even today I could actually just select a key-value pair like this

    var results = items0.Select((item) => {
        return (key:ComplexLogicForKey0(item),value: item);
    }).Join(items1.Select((item) => {
        return (key:ComplexLogicForKey1(item),value: item);
    }),(outer) => outer.key,(inner) => inner.key,(key,outer,inner) => (outer.key,outer.value,inner.value));

    This proposal is just for convenient and compactness, because the Join function itself always know the key to comparing equality so it should just be able to pass along

  3. Clockwork-Muse commented on Feb 20, 2019

    @Clockwork-Muse
    Contributor

    Well yeah.

    What I meant was, would your program do better if item already had key calculated/stored inside of it?

  4. Thaina commented on Feb 20, 2019

    @Thaina
    Author

    @Clockwork-Muse If it would be used only once when joining then why should I calculate and store it beforehand?

    Why do you think we have Lazy ?

    Why it should take up memory when it not being used until calling Join function?

    With my proposal, if it not being matched with anything then it would be popped out immediately

    It almost always have more advantage to just do something when it really needed. Why you are trying to think about anything unnecessary here? Why you need to try to thinking about doing anything when it really not necessary like caching something before really using it

    Why???

    Really you should ask yourself first that why you should cache value instead of argue with me why we should pass a value that would be very efficient to just pass what it already just being gotten

  5. Clockwork-Muse commented on Feb 20, 2019

    @Clockwork-Muse
    Contributor

    .... what I was curious about was whether or not this value, whatever it was, was truly used once only. Without more knowledge of your usecase, my default assumption tends to be that it's probably used more widely. You could of course implement it with a Lazy<T> - the exact first use wasn't at issue.

    Side note:

    With my proposal, if it not being matched with anything then it would be popped out immediately

    If you mean, "if there's no match between the left and the right operands, then the result is dropped immediately", that can only happen when the iterator is disposed. Like I said before, it has to calculate the key for all elements of the right operand before it can return the first row, and they have to hang around until the last element of the left operand is processed. Granted, yes, left elements are dropped after it moves to the next, so it might be beneficial there.

  6. Thaina commented on Feb 21, 2019

    @Thaina
    Author

    @Clockwork-Muse That's the point to not cache anything. The Join function should do thing as efficient as possible for the assumption of only once

    I was thinking from the start that if the logic will need to cache anything, it will be caching internally anyway. If we cache it beforehand then it means we have unnecessary copy of what will be cached again

    And complex logic might not be inefficient, just hard to write. It could be just a math and switch operation which just take a long line but could be run very fast. There are many reason that taking what already there is just better

  7. transferred this issue fromdotnet/corefxon Feb 1, 2020
  8. added this to the Future milestone on Feb 1, 2020
  9. added
    untriaged
    on Feb 23, 2020
  10. removed
    untriaged
    on Sep 2, 2020
  11. eiriktsarpalis commented on Nov 23, 2020

    @eiriktsarpalis
    Member

    As already mentioned, it is fairly straightforward to encode the desired behaviour on top of the existing join implementation:

        public static class Enumerable
        {
            public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TKey, TOuter, TInner, TResult> resultSelector)
            {
                return outer
                    .Select(outer => (key: outerKeySelector(outer), value: outer))
                    .Join(inner, (p => p.key), innerKeySelector, (pair, inner) => resultSelector(pair.key, pair.value, inner));
            }
        }

    It does not seem like this request is sufficiently popular to consider adding it to System.Linq. As such, I am going to close this issue. Thank you for your contribution!

  12. ghost locked as resolved and limited conversation to collaborators on Dec 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-System.Linq

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions