Edward Tanguay
Edward Tanguay

Reputation: 193402

How can I get a randomized collection out of linq-to-sql model?

What's the right syntax for this?

var words= from h in db.Words
                  orderby(a => Guid.NewGuid()).ToList()) //error
                  select h;

var words= from h in db.Words
                  orderby((a => Guid.NewGuid()).ToList()) //error
                  select h;

var words= from h in db.Words
                  orderby(Guid.NewGuid()) //no error but doesn't sort
                  select h;

Upvotes: 0

Views: 335

Answers (1)

CSharper
CSharper

Reputation: 236

Assuming that you don't mind not having all of your code embedded in the LINQ query, you can try this:

Random rnd = new Random();
var randomWords = from h in db.Words
                     orderby rnd.Next()
                     select h;

Though if you need the Guid approach:

var words = from h in db.Words
            orderby Guid.NewGuid()
            select h;

Upvotes: 3

Related Questions