Datatable. Select Method

Source: Internet
Author: User

The datatable. Select method returns an array of all datarow objects. Select () has four overload methods.

  Name Description
Select () Obtains the array of all datarow objects.
Select (string) Obtain the array of all datarow objects that match the filtering conditions according to the primary key order (if no primary key exists, the addition order is followed.
Select (string, string) Obtains an array of all datarow objects in the specified sorting order that matches the filtering conditions.
Select (string, String, dataviewrowstate) Obtains the array of all datarow objects that match the filters in the sorting order and the specified status.

 

1. Select () Example:

Private
Void getrows ()
{
// Get the datatable of a dataset.
Datatable table = dataset1.tables ["Suppliers"];
Datarow [] rows = table. Select ();

// Print the value one column of each datarow.
For (INT I = 0; I <rows. length; I ++)
{
Console. writeline (rows [I] ["companyName"]);
}
}

 

2. Select (string
Filterexpression) Example:

Private
Void getrowsbyfilter ()
{
Datatable table = dataset1.tables ["orders"];
// Presuming the datatable has a column named date.
String expression;
Expression = "date >#1/1/00 #";
Datarow [] foundrows;

// Use the select method to find all rows matching the filter.
Foundrows = table. Select (expression );

// Print column 0 of each returned row.
For (INT I = 0; I <foundrows. length; I ++)
{
Console. writeline (foundrows [I] [0]);
}
}

3. Select (String filterexpression, string sort) Example:

Private
Void getrowsbyfilter ()
{
Datatable table = dataset1.tables ["orders"];

// Presuming the datatable has a column named date.
String expression =
"Date> '2014/1/00 '";

// Sort Descending by column named companyName.
String sortorder =
"CompanyName DESC ";
Datarow [] foundrows;

// Use the select method to find all rows matching the filter.
Foundrows = table. Select (expression, sortorder );

// Print column 0 of each returned row.
For (INT I = 0; I <foundrows. length; I ++)
{
Console. writeline (foundrows [I] [0]);
}
}
4. Select (String filterexpression, string sort, dataviewrowstate
Recordstates) Example:

 


Private
Static void getrowsbyfilter ()
{
Datatable customertable = new datatable ("customers ");
// Add Columns
Customertable. Columns. Add ("ID", typeof (INT ));
Customertable. Columns. Add ("name", typeof (string ));

// Set primarykey
Customertable. Columns ["ID"]. Unique =
True;
Customertable. primarykey = new datacolumn []

{Customertable. Columns ["ID"]};

// Add ten rows
For (INT id = 1; id <= 10; Id ++)
{
Customertable. Rows. Add (
New object [] {ID,
String. Format ("customer {0}", ID )});
}
Customertable. acceptchanges ();

// Add another ten rows
For (INT id = 11; id <= 20; Id ++)

{

Customertable. Rows. Add (
New object [] {ID,
String. Format ("customer {0}", ID )});
}

String expression;
String sortorder;

Expression = "id> 5 ";
// Sort Descending by column named companyName.
Sortorder = "name DESC ";
// Use the select method to find all rows matching the filter.
Datarow [] foundrows =
Customertable. Select (expression, sortorder,
Dataviewrowstate. Added );

Printrows (foundrows, "filtered rows ");

Foundrows = customertable. Select ();
Printrows (foundrows, "All rows ");
}

Private
Static void printrows (datarow [] rows,
String label)
{
Console. writeline ("/n {0}", label );
If (rows. Length <= 0)
{
Console. writeline ("No rows found ");
Return;
}
Foreach (datarow row
In rows)
{
Foreach (datacolumn Column
In row. Table. columns)
{
Console. Write ("/table {0}", row [column]);
}
Console. writeline ();
}
}

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.