How to get a Random Object using Linq

前端 未结 9 1483
心在旅途
心在旅途 2020-12-29 02:34

I am trying to get a random object within linq. Here is how I did.

//get all the answers
var Answers = q.Skip(1).Take(int.MaxValue);
//get the random number         


        
9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-29 03:17

    Use a Fisher-Yates-Durstenfeld shuffle.

    (You could use a helper/extension method to shuffle your IEnumerable sequence. Alternatively, if you were using an IList you could perform an in-place shuffle, if you prefer.)

提交回复
热议问题