开发者

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

开发者 https://www.devze.com 2022-12-16 11:19 出处:网络
What\'s the right syntax for this? var words= from h in db.Words orderby(a => Guid.NewGuid()).ToList()) //erro开发者_如何转开发r

What's the right syntax for this?

var words= from h in db.Words
                  orderby(a => Guid.NewGuid()).ToList()) //erro开发者_如何转开发r
                  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;


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号