C # DataSet performance best practices

Source: Internet
Author: User

C # DataSet performance best practices

1. Use ItemArray to assign values to DataRow in batches

 

When assigning values to all fields of DataRow, it is inefficient to assign values by column using field names. In this case, use batch field assignment whenever possible. You can use ItemArray or rows. Add:
/Ds is the data set (DataSet) object DataTable dt = ds. tables [0]; DataRow row = dt. newRow (); row. itemArray = new object [] {value1, value2 ,..., Valuen}; // ds is the data set (DataSet) object DataTable dt = ds. Tables [0]; dt. Rows. Add (value1, value2 ,..., Valuen); // avoid making a large number of consecutive Single Column assignments, as shown below: DataTable dt = ds. tables [0]; DataRow row = dt. newRow (); row [col1] = value1; row [col2] = value2 ;... Row [coln] = valuen;

2. reasonably use the parallel computing of DataTable

 

DataTable's built-in parallel computing can make full use of each CPU of the computer to optimize the efficiency.
IEnumerable
 
  
FindRows () // find all the entries whose quantity is less than 0 {DataTable dt = ItemDataTable ;...... Return dt. Select ("Quantity <0"); // parallel computing not used} IEnumerable
  
   
FindRows () // find all the entries whose quantity is less than 0 {DataTable dt = ItemDataTable ;...... Int index = dt. columns. indexOf (Quantity); return dt. asEnumerable (). asParallel (). where (dr => (decimal) dr [index] <0); // use parallel computing :}
  
 

 

 

According to the experiment, parallel computing is better than Select and loop filtering when selecting the row of the DataTable; The performance is similar when the row traversal is performed.

 

3. Use ImportRow to merge data tables with the same structure

 

The Merge method can be used to Merge DataTable conveniently, but Merge is very inefficient in code. The example is as follows:
DataTable[] srcTables = ... ;foreach(DataTable src in srcTables ){dest.Merge( src ) ;}
ImportRow can also be used to Merge able, which has a much higher performance than Merge. The sample code is as follows:
DataTable[] srcTables = ... ;foreach(DataTable src in srcTables ){  foreach(DataRow row in src.Rows)  {     dest.ImportRow( row ) ;        }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.