Randomly ordered IEnumerable changes order each time it is used, instead of being reordered once

I am shuffling an IEnumerable using OrderBy and Random() and then selecting some of them using ElementAt.

Simplified version:

var r = new Random(DateTime.Now.Millisecond);
IEnumerable<IPublishedContent> testimonialsShuffled = testimonials.OrderBy(x => r.Next());

var testimonialA = testimonialsShuffled.ElementAt(0);
var testimonialB = testimonialsShuffled.ElementAt(1);
var testimonialC = testimonialsShuffled.ElementAt(2);

It turns out that this does not always return three distinct elements as I expected, because every time testimonialsShuffled is used, it seems to get reordered.
var testimonialA = testimonialsShuffled.ElementAt(0);
var testimonialB = testimonialsShuffled.ElementAt(0);
var testimonialC = testimonialsShuffled.ElementAt(0);

Similarly, I would expect this code to result in three equal variables, but instead they usually differ (as the first element in testimonialsShuffled changes each time).

My goal is just order my IEnumerable randomly once (so that its order changes on different page loads, but not multiple times in a single view). What am I doing wrong?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

OrderBy results in an IEnumerable, which is re-evaluated every time you access it (simply put).
To get one fixed result, you have to “force” it into a list.

var r = new Random(DateTime.Now.Millisecond);
var testimonialsShuffled = testimonials.OrderBy(x => r.Next()).ToList();

var testimonialA = testimonialsShuffled.ElementAt(0);
var testimonialB = testimonialsShuffled.ElementAt(1);
var testimonialC = testimonialsShuffled.ElementAt(2);

should do the trick.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x