Copied to clipboard

Flag this post as spam?

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


  • Morten Peter Hagh Jensen 12 posts 32 karma points
    Sep 12, 2017 @ 12:16
    Morten Peter Hagh Jensen
    0

    Random and Max from archetype

    Is it possible somehow to randomize the order and take 1 item from an archetype fieldset ?

    I have this now

    @{
        var testimonials = Umbraco.ContentSingleAtXPath("//HomePage").GetPropertyValue<ArchetypeModel>("testimonialItems");
    
        foreach (var item in testimonials) 
        {
            <h2>@item.GetValue("testimonalItemHeader")</h2>
    
            @item.GetValue("testimonalItemText")
    
             <q> @item.GetValue("testimonalItemName")</q>
        }
    }
    

    I have tried with

     var testimonials = Umbraco.ContentSingleAtXPath("//HomePage").GetPropertyValue<ArchetypeModel>("testimonialItems").RandomOrder().Take(1);
    

    But I am getting an error that Take and RandomOrder is not a part of ArchetypeModel

  • Sean Mooney 130 posts 157 karma points c-trib
    Sep 12, 2017 @ 15:47
    Sean Mooney
    0

    Have you tried this:

     var testimonials = Umbraco.ContentSingleAtXPath("//HomePage").GetPropertyValue<ArchetypeModel>("testimonialItems").OrderBy(x => Guid.NewGuid()).Take(1);
    
  • Morten Peter Hagh Jensen 12 posts 32 karma points
    Sep 12, 2017 @ 16:51
    Morten Peter Hagh Jensen
    0

    When using the above I am getting

    error CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
    
  • Sean Mooney 130 posts 157 karma points c-trib
    Sep 12, 2017 @ 17:23
    Sean Mooney
    0

    what about this? (sorry I don't currently have a project with Archetype)

     var testimonials = Umbraco.ContentSingleAtXPath("//HomePage").GetPropertyValue<ArchetypeModel>("testimonialItems").Fieldsets.OrderBy(x => Guid.NewGuid()).Take(1);
    
  • Morten Peter Hagh Jensen 12 posts 32 karma points
    Sep 12, 2017 @ 17:26
    Morten Peter Hagh Jensen
    0

    That didn't work either. Getting the same error.

  • Sean Mooney 130 posts 157 karma points c-trib
    Sep 12, 2017 @ 18:19
    Sean Mooney
    0

    OK, one last shot:

    var testimonials = Umbraco.ContentSingleAtXPath("//HomePage").GetPropertyValue<ArchetypeModel>("testimonialItems").ToList().OrderBy(x => Guid.NewGuid()).Take(1);
    
  • Morten Peter Hagh Jensen 12 posts 32 karma points
    Sep 13, 2017 @ 06:10
    Morten Peter Hagh Jensen
    0

    Hmm... That didn't work either. Same error!

    Thanks for the help anyway! :)

  • Dave Woestenborghs 2381 posts 6716 karma points mvp c-trib
    Sep 13, 2017 @ 06:35
    Dave Woestenborghs
    0

    Hi Peter,

    Can you try this.

    var testimonials = Umbraco.ContentSingleAtXPath("//HomePage").GetPropertyValue<ArchetypeModel>("testimonialItems").ToList().RandomOrder().Take(1);
    

    If it's not working, can you post the exact error

    Dave

  • Morten Peter Hagh Jensen 12 posts 32 karma points
    Sep 13, 2017 @ 06:39
    Morten Peter Hagh Jensen
    0

    Inserting this I am getting the error:

    Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Archetype.Models.ArchetypeModel' does not contain a definition for 'ToList'
    

    the same with Take or RandomOrder

  • Dave Woestenborghs 2381 posts 6716 karma points mvp c-trib
    Sep 13, 2017 @ 11:51
    Dave Woestenborghs
    0

    Hi

    Can you try this :

    var testimonials = Umbraco.ContentSingleAtXPath("//HomePage").GetPropertyValue<ArchetypeModel>("testimonialItems").Fieldsets.ToList().RandomOrder().Take(1);
    
  • Morten Peter Hagh Jensen 12 posts 32 karma points
    Sep 13, 2017 @ 12:29
    Morten Peter Hagh Jensen
    0

    Hi Dave,

    Just the same error.

    Am I missing something?

Please Sign in or register to post replies

Write your reply to:

Draft