The problem is that you are only using one list instead of creating a new list for each item. Adding the list to the hash table doesn't create a copy of the list, it just adds the reference. When you clear the list you will clear the list for all previously added items in the hash table, because they are all the same list.
You can create a new list and add to the hash table when you start a new group. As you keep the reference to the list, you can keep adding numbers to it after it is places in the hash table:
public static Hashtable GetAllProductIDsInAllRevenueGroups()
{
var productIDs;
var ht = new Hashtable();
string sql = @" {my sql here}";
var dt = Utilities.GetDataTableForQuery(sql, null);
int counter = 0;
int revenueGroupID = 0;
int lastRevenueGroupID = 0;
foreach (DataRow row in dt.Rows)
{
revenueGroupID = Utilities.SafeInt(row["RevenueGroupID"]);
int productID = Utilities.SafeInt(row["ProductID"]);
if (counter == 0 || revenueGroupID != lastRevenueGroupID)
{
productIDs = new List<int>();
ht.Add(revenueGroupID, productIDs);
}
productIDs.Add(productID);
lastRevenueGroupID = revenueGroupID;
counter++;
}
return ht;
}
Note: Consider using the strictly typed Dictionary<int, List<int>>
instead of Hashtable
.