Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Is it possible to use OrderBy to sort child nodes randomly?
e.g.
foreach (var item in Model.Descendants().OrderBy("random"){
Hi Dan
I think the approach should be done a bit like Rasmus is showing in this post http://our.umbraco.org/forum/developers/razor/35398-display-random-node (Just click go to solution).
You just need to set the max value high enough to make sure all your nodes are sorted random.
Hope this is what you're looking after.
/Jan
one of the suggestions was to do the following which should get you out of trouble
var items = Model.Descendants().OrderBy(i => Guid.NewGuid()).ToList();foreach(var item in items){
Ben
Jan - thanks but your solution is for a single node. I need to order a collection randomly.
Ben - this is what I need but am getting this error:
Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
Any ideas?
Thanks
Dan
Worked it out:
foreach (var item in Model.NodeById(sourceId).Descendants().Where("NodeTypeAlias == \"APerfectCVTestimonial\"").Random(@Model.NodeById(sourceId).Descendants().Where("NodeTypeAlias == \"APerfectCVTestimonial\"").Count())){
You can add an exstention method in your App_Code:
Extensions.cs
using System; using System.Collections.Generic; using System.Linq;namespace Extensions {public static IEnumerable Randomize(this IEnumerable source){ Random rnd = new Random(); return source.OrderBy((item) => rnd.Next());}}
Then in your macros you can do this:
@using Extensions
foreach(var item inModel.Descendants().Randomize()){}
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Sorting a collection randomly
Is it possible to use OrderBy to sort child nodes randomly?
e.g.
Hi Dan
I think the approach should be done a bit like Rasmus is showing in this post http://our.umbraco.org/forum/developers/razor/35398-display-random-node (Just click go to solution).
You just need to set the max value high enough to make sure all your nodes are sorted random.
Hope this is what you're looking after.
/Jan
one of the suggestions was to do the following which should get you out of trouble
Ben
Jan - thanks but your solution is for a single node. I need to order a collection randomly.
Ben - this is what I need but am getting this error:
Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
Any ideas?
Thanks
Dan
Worked it out:
You can add an exstention method in your App_Code:
Extensions.cs
Then in your macros you can do this:
is working on a reply...