Sunday, October 27, 2013

Randomizing rows – C#

In earlier post we have seen how we can randomize rows using sql server function “NewId()”.
 Now we will explore way of randomizing rows in C# after retrieving them from database.
I am writing a console application to illustrate the concept
My console application contain a class for generating random number, in main program class I have a function to get dataset “GetDataSet” that emulate fetching data from SQL server using ADO.NET .
The Idea is to add one more row into retrieved record set and put random number value there ,then based on this column sorting whole datatable.
   I am using LINQ to achieve it.       
Below is complete code for the same.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Threading;

namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            DataSet ds = GetDataSet();
             //Copying A Schema of Existing Result Reset and adding one more column to 
            //save randomly generated number
            DataTable Mydt = new DataTable();
            Mydt = ds.Tables[0].Clone();
            Mydt.Columns.Add("Randomizer", typeof(Int32));

            //Generate specfied number of brandom numbers
            var RandomList = RandomProvider.GetRandomSequence(100, ds.Tables[0].Rows.Count).ToList();
            //used to keep track of row number in linq query
            int i=0;

            //doing linq operations on datarow collection
             IEnumerable<DataRow> results =(
                                                                new Object[] {
                                            ).ToList().OrderBy(x => x.Field<Int32>("Randomizer"));

            //Final Output
             Mydt = results.CopyToDataTable();

        public static DataSet GetDataSet()
            DataSet ds = new DataSet();

            DataTable dt = new DataTable("ExampleDt");
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("City", typeof(string));
            dt.Columns.Add("Company", typeof(string));

            DataRow drow1 = dt.NewRow();
            drow1["Id"] = 1;
            drow1["Name"] = "suresh";
            drow1["City"] = "Mumbai";
            drow1["Company"] = "ABC Pvt Ltd.";

            DataRow drow2 = dt.NewRow();
            drow2["Id"] = 2;
            drow2["Name"] = "Ramesh";
            drow2["City"] = "Nagpur";
            drow2["Company"] = "PQR Pvt Ltd.";

            DataRow drow3 = dt.NewRow();
            drow3["Id"] = 3;
            drow3["Name"] = "Jayesh";
            drow3["City"] = "Delhi";
            drow3["Company"] = "LMN Pvt Ltd.";

            DataRow drow4 = dt.NewRow();
            drow4["Id"] = 4;
            drow4["Name"] = "Mahesh";
            drow4["City"] = "Banglore";
            drow4["Company"] = "JKL Pvt Ltd.";


            return ds;
     public static class RandomProvider
        public static IEnumerable<int>  GetRandomSequence(int maxNumber)
            var random = new Random();
            while (true)
                yield return random.Next(maxNumber);

        public static IEnumerable<int> GetRandomSequence(int maxNumber, int maxCount)
            return GetRandomSequence(maxNumber).Take(maxCount);


No comments:

Post a Comment