How to extract 10 random rows from DataTable?

前端 未结 3 1956
旧巷少年郎
旧巷少年郎 2021-01-12 00:19

Let\'s say I have a DataTable with ~50 rows (GetDataTable() on a list in SharePoint). I want to keep 10 random rows and forget about the rest. How can I achieve this?

<
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-12 01:20

    You can use a Fisher/Yates shuffle (implementation by Skeet) on the collection of rows on the DataTable, then select the first 10.

    var random10 = dataTable.Rows.OfType().Shuffle(new Random()).Take(10);
    

提交回复
热议问题