1

I have a User object that has a collection of Items.

How can I randomize the Items so they dont' appear in the exact same order everytime.

I'm currently ordering by SortOrder (integer), but this will obviously be in the same order everytime.

@foreach(UserItems ui in Model.User.Items.OrderBy(x => x.SortOrder)) { } 
0

1 Answer 1

3

Here's a little trick:

@foreach(UserItems ui in Model.User.Items.OrderBy(x => Guid.NewGuid())) { } 
Sign up to request clarification or add additional context in comments.

7 Comments

+1 for nice solution. But I am very curious how it is translated to T-SQL, do you know?
+1 for creative thinking
This may be sufficient for the asker's purposes, but it's important to note that this is not really random.
semi-random is fine. Does it matter if the items are lazy loaded or eager loaded?
@Alireza Take a look at this answer also. stackoverflow.com/a/4120132/205859. It looks like LINQ provider is able to translate it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.