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.