[Convert] datatable select () method

Source: Internet
Author: User

Datatable is a class we often use during development, and we often need to filter the data in the datatable. Next we will introduce a common method in datatable-select, microsoft provides four function overloading, which are

 

Select ()

Select (string filterexpression)

Select (string filterexpression, string sort)

Select (string filterexpression, string sort, dataviewrowstate record states ).

 

1) Select () -- Obtain the array of all system. Data. datarow objects.

2) Select (string filterexpression) -- obtains an array of all system. Data. datarow objects that match the filtering conditions in the primary key order (if no primary key exists, the array is added.

3) Select (string filterexpression, string sort) -- obtains an array of all system. Data. datarow objects in the specified sorting order that matches the filtering conditions.

4) Select (string filterexpression, string sort, dataviewrowstate recordstates) -- obtains the array of all system. Data. datarow objects that match the filter in the sorting order and the specified status.

 

The following is an example of how to demonstrate these methods:

Using system;

Using system. Collections. Generic;

Using system. text;

Using system. Data;

 

Namespace testdatatableselect

{

Class Program

{

Static datatable dt = new datatable ();

Static void main (string [] ARGs)

{

Datacolumn DC1 = new datacolumn ("ID ");

Dc1.datatype = typeof (INT );

Datacolumn DC2 = new datacolumn ("name ");

Dc2.datatype = typeof (system. String );

DT. Columns. Add (DC1 );

DT. Columns. Add (DC2 );

For (INT I = 1; I <= 10; I ++)

{

Datarow DR = DT. newrow ();

If (I <= 5)

{

Dr [0] = I;

Dr [1] = I + "--" + "hello ";

}

Else

{

Dr [0] = I;

Dr [1] = I + "--" + "nihao ";

}

DT. Rows. Add (DR );

}

 

Select ();

Select ("ID> = '3' and name = '3 -- hello '");//SupportedAnd

Select ("ID> = '3' or ID = '1 '");//SupportedOr

Select ("name like '% Hello % '");//SupportedLike

Select ("ID> 5", "id DESC ");

Select ("ID> 5", "id DESC", dataviewrowstate. Added );

}

 

Private Static void select ()

{

Datarow [] arraydr = DT. Select ();

Foreach (datarow DR in arraydr)

{

Console. writeline (Dr [0]. tostring () + "" + Dr [1]. tostring ());

}

Console. Readline ();

}

 

Private Static void select (string filterexpression)

{

Datarow [] arraydr = DT. Select (filterexpression );

Foreach (datarow DR in arraydr)

{

Console. writeline (Dr [0]. tostring () + "" + Dr [1]. tostring ());

}

Console. Readline ();

}

 

Private Static void select (string filterexpression, string sort)

{

Datarow [] arraydr = DT. Select (filterexpression, sort );

Foreach (datarow DR in arraydr)

{

Console. writeline (Dr [0]. tostring () + "" + Dr [1]. tostring ());

}

Console. Readline ();

}

 

Private Static void select (string filterexpression, string sort, dataviewrowstate recordstates)

{

Datarow [] arraydr = DT. Select (filterexpression, sort, recordstates );

Foreach (datarow DR in arraydr)

{

Console. writeline (Dr [0]. tostring () + "" + Dr [1]. tostring ());

}

Console. Readline ();

}

}

}

Note: The preceding select operation is case-insensitive (record fields are not sensitive). If case-sensitive, set the casesensitive attribute of datatable to true.

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.