Sorry if the question title was a bit weird. I want to populate 500 excel rows with a composite primary key which consists of 3 columns. 2 columns automatically generate random int between 1 and 50 and third is a date between 01.01.2006 and 31.12.2013. So i want to have 500 rows, each with a different combination of the 3. Here's my code
Type excelType = Type.GetTypeFromProgID("Excel.Application");
dynamic excel = Activator.CreateInstance(excelType);
excel.visible = true;
excel.Workbooks.Add();
Random rnd = new Random();
dynamic sheet = excel.ActiveSheet;
for (int i = 1; i <= 500; i++)
{
sheet.Cells[i, "A"] = rnd.Next(1,50);
sheet.Cells[i, "B"] = rnd.Next(1,50);
sheet.Cells[i, "C"] = RandomDay();
// this is where I'd check if a combination exists and if it does assign a new one
for (int j = 0; j <= i + 1; j++)
{
if ( sheet.Cells[j + 1, "A"] == sheet.Cells[i, "A"] &&
sheet.Cells[j + 1, "B"] == sheet.Cells[i, "B"] &&
sheet.Cells[j + 1, "C"] == sheet.Cells[i, "C"])
{
sheet.Cells[i, "A"] = rnd.Next(1,50);
sheet.Cells[i, "B"] = rnd.Next(1,50);
sheet.Cells[i, "C"] = RandomDay();
}
}
}
}
// random date method
public static DateTime RandomDay()
{
DateTime start = new DateTime(2006, 1, 1);
DateTime end = new DateTime(2013, 12, 31);
Random gen = new Random();
int range = (end - start).Days;
return start.AddDays(gen.Next(range));
}
I'm really not sure if this would work, plus it's running slow, it has to iterate over and over again to check if the combination exists. Does anyone have a better and faster solution? Thank you all!