How to return datatable by executing able. Select ("condition") in datatable [reprinted]

Source: Internet
Author: User

In actual programming projects, we often encounter the following situation: datatable is not in the database, or datatable has not been written to the database, or the able read from the database has been changed locally, I have not written it back to the database (other changes may be required). In these cases, I only need to use it. the powerful functions of the dataview class provided by the net class library (mainly using its rowfilter attribute) can easily solve such query problems.
Some netizens asked online how to execute able. Select ("condition") in datatable to return the datatable. Today I am here to provide a solution for your reference:
CodeAs follows:

  /// / <Summary>
  /// Execute the query in the datatable to return the new datatable
  ///   </Summary>
  ///   <Param name = "DT"> Source data datatable </Param>
  ///   <Param name = "condition"> Query Conditions </Param>
  ///   <Returns> </returns>
Private Datatable getnewdatatable (datatable DT, String Condition)
Datatable newdt =   New Datatable ();
Newdt = DT. Clone ();
Datarow [] Dr = DT. Select (condition );
For ( Int I = 0 ; I < Dr. length; I ++ )
Newdt. importrow (datarow) Dr [I]);
Return Newdt; // Returned query results


Hey, they are all the same. Add rows cyclically.

Datarow [] rows = DT. Select (conditions );
Foreach (Datarow row In Rows)
Newdt. Rows. Add (row. itemarray );
Return Newdt;

Davin: This afternoon, I also encountered this problem. Regarding the datatable filtering problem, I asked to retrieve the first 20 results after filtering, always hovering between the filterrow and select methods, filterrow has poor performance. Select () returns datarow [] is not easy to operate, but you can see your practice, DT. clone ();
The performance is really serious. If dt is a large data table
DT. Clone () should have no performance problems.
The clone () method only copies structure information and does not copy data.

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: 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.