Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 838365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:17:36+00:00 2026-05-15T05:17:36+00:00

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

  • 0

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?

Update: Seeking answers that do not modify the table.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:17:37+00:00Added an answer on May 15, 2026 at 5:17 am

    I’m not sure if this is best, but it beats sorting and then picking the top three elements which has time complexity O(n log n).

    You can use a priority queue to filter the top three elements. Information about .Net priority queue implementations is available here.

    The basic idea is to insert the first three elements of your data table into the priority queue. You then successively add all of remaining elements, removing the top element after each add. The elements remaining in the priority queue (heap) after that will be the top three elements.

    No modification to the table is needed, in terms of adding another column (you just need to define the relative ordering / priority criteria) and doesn’t change the order the table elements. Time complexity will be O(n log 3) = O(n).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a DataTable: DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn(Country, typeof(string)));
Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
Lets say have this immutable record type: public class Record { public Record(int x,
I have a DataGridView that is bound to a DataTable, it has a column
Let's say I have a class that has 3 properties. I don't know which
Let's say I have a class that has a MyCustomDatabaseAccess as a data member.
Lets say I have a massive project which consists of multiple dynamic libraries that
Lets say I have Class A, that calls static methods in either Class B
Lets say I have a class that is doing something like: public class Foo
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.