Compute method in dataset

I always use compute function to calculate data in some columns in the output datatable variable, so is there a way i can use if i want for example the sum of the top 10 rows in datatable, can i use compute, i donot want to return to the original table
Thanx
ashraf_tAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Ted BouskillConnect With a Mentor Senior Software DeveloperCommented:
Sorry but I'm more comfortable with C#
DataTable outputTable = SourceTable.Clone();
for(int i=0;i<10;i++)
  outputTable.ImportRow(SourceTable.Rows[i]);

Open in new window

0
 
Ted BouskillSenior Software DeveloperCommented:
The DataSet object actually uses a substantial amount of overhead (memory and CPU)  An aggregate operation can even consume more resources.  I would strongly recommend you do this in the SQL database.
0
 
CodeCruiserConnect With a Mentor Commented:
If you still prefer to do it in the memory then use the select function to select the top 10 rows and then use the compute.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
ashraf_tAuthor Commented:
no i want to use the top 10 in the output data not to the original table
0
 
ashraf_tAuthor Commented:
Looping method hmmm, i think this will take time specially if the records are too big
0
 
Ted BouskillSenior Software DeveloperCommented:
It's either that or do it in the SQL server.  You don't have any other choices.  The Select() method in the DataTable doesn't support aggregate functions.
0
 
Ted BouskillSenior Software DeveloperCommented:
I manage 5 software developers building web applications.  I wouldn't allow them to do this in a dataset at all.  I'm just trying to answer your question, I don't agree with the strategy.

If you are worried about performance, minimize or avoid using the dataset and use the SqlDataReader instead and define your own collection.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.