Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Matt Speakman 11 posts 44 karma points
    Jul 14, 2015 @ 15:11
    Matt Speakman
    1

    Umbraco.Core.EnumerableExtensions.ForAllThatAre

    Hello, We stumbled across

            public static IEnumerable<TBase> ForAllThatAre<TBase, TActual>(this IEnumerable<TBase> sequence, Action<TActual> projection) where TActual : class;
    

    which is an extension in the Umbraco.Core but cannot figure out what it does.

    Can anyone shed some light please?

    Thanks,

    Matt

  • Gary Devenay 39 posts 245 karma points
    Jul 14, 2015 @ 15:51
    Gary Devenay
    0

    Hi Matt,

    The implementation of this method can be found here.

    Hope this helps

    Gary

  • Matt Speakman 11 posts 44 karma points
    Jul 15, 2015 @ 13:07
    Matt Speakman
    0

    Gary, Thanks for replying but I'm still not sure what it actually does.

  • Gary Devenay 39 posts 245 karma points
    Jul 15, 2015 @ 13:33
    Gary Devenay
    0

    Hi Matt,

    If you could post your usage of this method, I may be able to explain it in some context.

    return sequence.Select( //Loop through each item in sequence
                    x =>
                    {
                        if (x is TActual) // If the current item TActual is derived from type TBase
                        {
                            var casted = x as TActual; // cast TBase to TActual
                            projection.Invoke(casted); // Invoke the action which you passed, passing our casted TActual as a parameter
                        }
                        return x; // Return the initial TBase
                    });
    

    Basically what this method allows us to do is loop through a list of objects and invoke an Action for each item which is of a certain base type without amending the initial list.

    The method is extremely generic which may have caused some confusion, but the above is a basic explanation of how it could be used.

  • Matt Speakman 11 posts 44 karma points
    Jul 16, 2015 @ 08:23
    Matt Speakman
    0

    Gary, thanks for getting back to me. I haven't used the method yet as I didn't know what to do with it. Am I right in saying that we could give it a collection of objects and sort them based on whether they are a string?

Please Sign in or register to post replies

Write your reply to:

Draft