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.