Select () usage in datatable

Source: Internet
Author: User
1. Execute datatable. Select ("condition") in datatable to return datatable;

// <Summary>
// Execute the query in the datatable to return the new datatable
/// </Summary>
// DT is the source data datatable
// Condition is the query Condition

Datatable newdt = new datatable ();
Newdt = DT. Clone (); // clone the DT structure, including all DT architectures and constraints without data;
Datarow [] rows = DT. Select (conditions); // query matching records from DT;
Foreach (datarow row in rows) // Add the query result to DT;
{
Newdt. Rows. Add (row. itemarray );
}

Some netizens said they could do the same: (you can try it)

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]);
}

2. About datatable. Select ();

Select method:
Select (); // check all
Select (filter condition); // filter by filtering condition, such as select ("columnname1 like '% XX % '");
Select (filter condition, sorting field); // filter and sort, such as select ("columnname1 like '% XX %'", columnname2 );

After a query is completed and a datatable is returned, you often want to continue searching in the query results. In this case, you can use the datatable. Select method to re-query the results.
The select method has four overload values. We often use datatable. Select (string );

The following describes the datatable. Select (string) with a parameter ):
The string parameter is a query limitation. It is equivalent to the where Statement (excluding where) in the SQL query language. Its Syntax conforms to the SQL language syntax. I think it is similar to the SQL syntax.

However, I tried it and it does not support between and. For example:

// Fromtime and totime are two datetime-type variables; occurtime is the column name in dtable;

Datarow [] datarows = dtable. Select ("occurtime> = '" + fromtime + "' and occurtime <= '" + totime + "'");

The datatable. Select () method supports simple filtering and sorting, and does not support complex conditional filtering and sorting. The strings must be column names and data, and Relational operators such as >,<,=, <>. For example:

Datarow [] ROW = detailtb. select ("wzmc = '" + materialname + "' and cz = '" + materialtexture + "and GG ='" + materialspecs + "'");

Datatable. Select ("city like 'B % '");

Datatable. Select ("name = '" + A + "'");

Be sure to pay attention to the single quotation marks. I used to enclose the variables in double quotation marks and keep making mistakes. Later I checked the variables online and found that double quotation marks should be used in single quotation marks; that is, 'variable "';

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.