linq select a random row

后端 未结 6 1419
悲&欢浪女
悲&欢浪女 2020-12-30 03:05

I have a table called Quotes in linq-to-sql that contains 2 columns: author and quote. How do you select both columns of a random row?

6条回答
  •  庸人自扰
    2020-12-30 03:48

    Here is one way to achieve what you want to do:

    var quotes = from q in dataContext.Quotes select q;
    int count = quotes.Count();
    int index = new Random().Next(count);
    var randomQuote = quotes.Skip(index).FirstOrDefault();
    

提交回复
热议问题