hiee every one, i am developing a small application from where all questions are coming randomly.
i have use the default random method to get random data but it gives duplicate rows twice. so what i need is to get distinct rows into data table.
i am using following method to get rendom records…
Random rDom = new Random();
dtRandom = dt.Clone();
int rw = 0;
for (int ctr = 1; ctr <= dt.Rows.Count; ctr++)
{
rw = rDom.Next(1, dt.Rows.Count);
dtRandom.ImportRow(dt.Rows[rw]);
}
dtRandom.AcceptChanges();
so, how can i achieve distinct records from datatable ?
As you are aware, random number generation may generate the same number multiple times. Either check for the existence of the imported row before importing it into your datatable, if there is a unique surrogate key / primary key. Otherwise you can use this approach to get the distinct values from the final DataTable:
From http://msdn.microsoft.com/en-us/library/wec2b2e6.aspx