linq select a random row

后端 未结 6 1394
悲&欢浪女
悲&欢浪女 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:45

    I did it something like this:

    list.ElementAt(rand.Next(list.Count());
    

    I stuck a bunch of random operations, including select and shuffle, as extension methods. This makes them available just like all the other collection extension methods.

    You can see my code in the article Extending LINQ with Random Operations.

提交回复
热议问题