linq select a random row
Random rand = new Random();
int toSkip = rand.Next(0, context.Quotes.Count);
context.Quotes.Skip(toSkip).Take(1).First();
If you're doing Linq-to-Objects and don't need this to work on SQL, you can use ElementAt()
instead of the more verbose Skip(toSkip).Take(1).First()
:
var rndGen = new Random(); // do this only once in your app/class/IoC container
int random = rndGen.Next(0, context.Quotes.Count);
context.Quotes.ElementAt(random);