0

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;
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047

1 Answers1

3

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;
CSharper
  • 236
  • 1
  • 3
  • The first version would work for LINQ-to-Objects, but not LINQ-to-SQL surely? I'd have to check with the second version, but I didn't *think* that worked... – Marc Gravell Jan 17 '10 at 10:19