Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
First of all, can't use LINQ or lambda expressions because i am being forced into using .NET version 1.1 and there is no support for those type of operations.

I am querying my database using a DataReader and get the resultset

ORDERLINK   COMBINATIONLINK QTY
----------- --------------- -----------
197884      1765649         1405
197884      1765658         1275
197884      1765661         2384
358390      4398583         50
358390      2385397         100


I am using the SqlCommand/SqlDataReader method of querying for, and then reading, the resultset above. What I want is -- in code, no LINQ/lambda, and can't make any more db calls -- my code to give me a 'resultset' (like in an ArrayList) like

ORDERLINK   QTY
----------- -----------
197884      5604
358390      150


I have to use the COMBINATIONLINK somewhere else so I can't just call a SQL GROUP BY on the first resultsset.

How do I get the second resultset?
Posted
Updated 25-May-12 12:07pm
v2
Comments
Sergey Alexandrovich Kryukov 25-May-12 18:33pm    
Tell you "boss" that really usable .NET starts with v.2.0; even though it also does not have lambda and hence no LINQ, at least it's stable enough.
Answering your question is no fun because -- who would bother trying it with v.1.1? -- it's not even listed in the list of target platforms.
Really sorry...
--SA

1 solution

Here you go.

If you like it, please mark answer accepted.

C#
public void DoGroupBy(/* DataSet ds */)
{
 int[] arrayOL = { 8, 8, 8, 9, 9 };
 int[] arrayQty = { 20, 30, 40, 50, 100 };
 //int nRows = ds.Tables[0].Rows.Count;
 int nRows = 5;
 int ORDERLINK, QTY;
 int oldOL = 0, SumQty = 0;
 Dictionary<int,int> result = new Dictionary<int,int>();
 for (int i = 0; i < nRows; ++i)
 {
  // inputs
  //ORDERLINK = (int)ds.Tables[0].Rows[i]["ORDERLINK"];
  ORDERLINK = arrayOL[i];
  //QTY = (int)ds.Tables[0].Rows[i]["QTY"];
  QTY = arrayQty[i];
  if (i > 0)
  {
   if (ORDERLINK == oldOL)
   {
    SumQty += QTY;
   }
   else
   {
    // ORDERLINK has changed
    result.Add(oldOL, SumQty);
    SumQty = QTY;
   }
  }
  else
  {
   SumQty = QTY;
  }
  oldOL = ORDERLINK;

 }
 result.Add(oldOL, SumQty);

 Console.WriteLine("ORDERLINK QTY");
 Dictionary<int,int>.Enumerator en = result.GetEnumerator();
 while (en.MoveNext())
 {
  Console.WriteLine(String.Format("{0}         {1}", en.Current.Key,   
                    en.Current.Value));
 }
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900