Most efficient sorting of calculation on DataTable column calculation

Posted by byte on Stack Overflow See other posts from Stack Overflow or by byte
Published on 2010-06-01T20:39:11Z Indexed on 2010/06/01 21:03 UTC
Read the original article Hit count: 347

Filed under:
|
|
|

Lets say you have a DataTable that has columns of "id", "cost", "qty":

DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("cost", typeof(double));
dt.Columns.Add("qty", typeof(int));

And it's keyed on "id":

dt.PrimaryKey = new DataColumn[1] { dt.Columns["id"] };

Now what we are interested in is the cost per quantity. So, in other words if you had a row of:

id | cost  | qty
----------------
42 | 10.00 | 2

The cost per quantity is 5.00.

My question then is, given the preceeding table, assume it's constructed with many thousands of rows, and you're interested in the top 3 cost per quantity rows. The information needed is the id, cost per quantity. You cannot use LINQ.

In SQL it would be trivial; how BEST (most efficiently) would you accomplish it in C# without LINQ?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET