I writing a line item and the values are like this
Ad# Pub# Loc date line#
ad1 P001 AK 093010 1
ad1 P001 AK 093010 2
ad1 P001 AK 093010 3
ad1 P001 AK 100110 1
When the ad#,Pub#, loc and date are same as the previous record the line is incremented. Once any of the values in ad,pub, loc and date changes compared to previous record value of line is changed to 1 again.
I am writing these lines in a for loop. I need to know what logic i need to use to get the line# values? Do i use an array to store values of previous line in an array and compare them with current line values and if they are same then add 1 to line#. Is there a better logic? Or is there a better way other than arrays to compare the previous line and current line
These values are got in from various tables.
for (int l = 0; l < totalrecords; l++)
{
for (int i = 0; i < fields.Length; i++)
{
if (i == 0)
{
// logic to get the value for field 1
}
else if (i == 1)
{
// logic to get the value for field 2
}
if (i == 3)
{
// logic to get the value for field 3
}
else if (i == 4)
{
// logic to get the value for field 4
}
else if (i == 5)
{
// What would be the logic to write the line#???
}
}
}
thanks
Prady
First off, I don’t know why you need the inner
iloop; Is the number of fields dynamic?Anyways, back to your question. Without knowing whether your source is DataTables, DataReaders, Arrays, etc, it’s hard to answer.
But yes, I’d remember the previous values between iterations and reset the line number when current values differ.
If using DataTable, I typically use this logic:
The benefit of this is that if you need to do anything at the end of each set, eg. totals, you can do that after the inner loop finishes, before looping around to start a new set.
I’m sure LINQ could make this easy, too.