mardi 26 juillet 2016

C# - How to select distinct random records from datatable

I am trying to fetch records from DataTable randomly with some limit. I am able to fetch the data randomly as follows -

 var r = new Random();
 var _randomSelection = dt.Rows.OfType<DataRow>().OrderBy(rand => r.Next()).Take(limit);

But the values seems to be redundant. I want to get the unique values. So far I have tried Distinct() as -

var _randomSelection = dt.Rows.OfType<DataRow>().OrderBy(rand => r.Next()).Take(limit).Distinct();

But that doesn't seems to be worked out. Can anybody please guide where am going wrong with this. Thanks.




Aucun commentaire:

Enregistrer un commentaire