Datatable. Select returns datatable

Source: Internet
Author: User

Datatable. Select ("condition") return datatable Solution 
ArticleSource: http://www.cnblogs.com/hermes262/archive/2006/12/06/583832.html

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 condition </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; // The returned query result.
}

--
PS:
The result of adding rows cyclically is the same:
Datarow [] rows = DT. Select (conditions );
Foreach (datarow row in rows)
{
Newdt. Rows. Add (row. itemarray );
}
Return newdt;
--

My ps vb. NET code:

Private function getnewdatatb (byval dT as datatable, byval condition as string) as datatable
Dim newdt as new datatable
Newdt = DT. Clone
Dim DR () as datarow = DT. Select (condition)
For I as integer = 0 to dr. Length-1
Newdt. importrow (ctype (DR (I), datarow ))
Next
Return newdt
End Function

This operation increases system resources, but reduces the amount of data read from the database.

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.