tags:

views:

88

answers:

2

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

+1  A: 

Probably the most idiomatic way of representing the values so that they can be easily compared would be to load them into some class and implement the IComparable<T> interface in that class.

The class declaration would look roughly like this:

class Item : IComparable<Item> { 
  public string Loc { get; private set; }
  public Item(Field[] fields) {
    Loc = // logic to get value of 'Loc'
    // Load all other values
  }
  public int CompareTo(Item other) {
    int res = Loc.CompareTo(other.Loc);
    if (res != 0) return res; 
    // .. compare other fields
  }
}

Then you can just iterate over all the records, create objects from them by writing new Item(fields). When you have two fields, you can compare them simply by writing f1.CompareTo(f2).

Tomas Petricek
+1  A: 

First off, I don't know why you need the inner i loop; 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:

var dt = ...
for(var rowIndex = 0; rowIndex < dt.Rows.Count;)
{
    // start of new set
    var curAd = (string)dt.Rows[rowIndex]["ad"];
    var curPub = (string)dt.Rows[rowIndex]["pub"];
    var curLoc = (string)dt.Rows[rowIndex]["loc"];
    var curDate = (DateTime)dt.Rows[rowIndex]["date"];
    var line = 1;

    for (; rowIndex < dt.Rows.Count;)
    {
        // end of set?
        if (curAd != (string)dt.Rows[rowIndex]["ad"] ||
           curPub != (string)dt.Rows[rowIndex]["pub"] ||
           curLoc != (string)dt.Rows[rowIndex]["loc"] ||
           curDate != (DateTime)dt.Rows[rowIndex]["date"])
        {
            break;
        }

        // write out current values with new line number
        // ...

        // get ready for next row
        rowIndex++;
        line++;
    }

    // key fields have changed, or eof, loop around to terminate or start new set as needed

}

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.

Will
yes, the field length varies depending on the parameters passed
Prady