Perform Grouping of Resultsets in Code, not on Database Level

Posted by NinjaBomb on Stack Overflow See other posts from Stack Overflow or by NinjaBomb
Published on 2010-04-12T22:16:23Z Indexed on 2010/04/26 14:53 UTC
Read the original article Hit count: 370

Filed under:
|
|
|
|

Stackoverflowers,

I have a resultset from a SQL query in the form of:

Category  Column2   Column3
A         2        3.50  
A         3        2  
B         3        2  
B         1        5  
...

I need to group the resultset based on the Category column and sum the values for Column2 and Column3. I have to do it in code because I cannot perform the grouping in the SQL query that gets the data due to the complexity of the query (long story). This grouped data will then be displayed in a table.

I have it working for specific set of values in the Category column, but I would like a solution that would handle any possible values that appear in the Category column.

I know there has to be a straightforward, efficient way to do it but I cannot wrap my head around it right now. How would you accomplish it?

EDIT

I have attempted to group the result in SQL using the exact same grouping query suggested by Thomas Levesque and both times our entire RDBMS crashed trying to process the query.

I was under the impression that Linq was not available until .NET 3.5. This is a .NET 2.0 web application so I did not think it was an option. Am I wrong in thinking that?

EDIT

Starting a bounty because I believe this would be a good technique to have in the toolbox to use no matter where the different resultsets are coming from. I believe knowing the most concise way to group any 2 somewhat similar sets of data in code (without .NET LINQ) would be beneficial to more people than just me.

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about ASP.NET