Introduction to the Select () method of the C # datatable

Source: Internet
Author: User

The DataTable is a class that we often use in the development process, and we often need to filter the data in the DataTable, and then introduce a method commonly used in the DataTable--select, Microsoft provides four functions of the overloaded, respectively, is

Select ()

Select (String filterexpression)

Select (String filterexpression, string sort)

Select (String filterexpression,string Sort, dataviewrowstate record states).

1) Select ()--Gets an array of all System.Data.DataRow objects.

2) Select (string filterexpression)-Gets an array of all System.Data.DataRow objects that match the filter criteria in the primary key order (if there is no primary key, in order of addition).

3) Select (string filterexpression, String sort)--Gets an array of all System.Data.DataRow objects that match the filter criteria in the specified sort order.

4) Select (string filterexpression, string sort, DataViewRowState recordstates)--gets all the matches of the filter in the sort order and the specified state An array of System.Data.DataRow objects.

The following is an example of a demonstration of these methods:

1 usingSystem;2 3 usingSystem.Collections.Generic;4 5 usingSystem.Text;6 7 usingSystem.Data;8 9  Ten  One namespaceTestdatatableselect A  - { -  the     class Program -  -     { -  +         StaticDataTable dt =NewDataTable (); -  +         Static voidMain (string[] args) A  at         {          -  -DataColumn DC1 =NewDataColumn ("ID"); -  -DC1. Datatype=typeof(int); -  inDataColumn DC2 =NewDataColumn ("name"); -  toDC2. Datatype=typeof(System.String); +  - dt. Columns.Add (DC1); the  * dt. Columns.Add (DC2); $ Panax Notoginseng              for(inti =1; I <=Ten; i++ ) -  the             { +  ADataRow dr =dt. NewRow (); the  +                 if(I <=5) -  $                 { $  -dr[0] =i; -  thedr[1] = i +"--"+"Hello"; - Wuyi                 } the  -                 Else Wu  -                 { About  $dr[0] =i; -  -dr[1] = i +"--"+"Nihao"; -  A                 } +  the dt. Rows.Add (DR); -  $             } the  the   the  the Select (); -  inSelect ("id>= ' 3 ' and name= ' 3--hello '");//Support and the  theSelect ("id>= ' 3 ' or id= ' 1 '");//Support or About  theSelect ("name like '%hello% '");//Support like the  theSelect ("id>5","ID desc"); +  -Select ("id>5","ID desc", dataviewrowstate.added); the Bayi         } the  the   -  -         Private Static voidSelect () the  the         { the  thedatarow[] Arraydr =dt. Select (); -  the             foreach(DataRow Drincharraydr) the  the             {94  theConsole.WriteLine (dr[0]. ToString () +"    "+dr[1]. ToString ()); the  the             }98  About console.readline (); - 101         }102 103  104  the         Private Static voidSelect (stringfilterexpression)106 107         {108 109datarow[] Arraydr =dt. Select (filterexpression); the 111             foreach(DataRow Drincharraydr) the 113             { the  theConsole.WriteLine (dr[0]. ToString () +"    "+ dr[1]. ToString ()); the 117             }118 119 console.readline (); - 121         }122 123  124  the         Private Static voidSelect (stringFilterExpression,stringsort)126 127         { - 129datarow[] Arraydr =dt. Select (filterexpression,sort); the 131             foreach(DataRow Drincharraydr) the 133             {134 135Console.WriteLine (dr[0]. ToString () +"    "+ dr[1]. ToString ());136 137             }138 139 console.readline (); $ 141         }142 143  144 145         Private Static voidSelect (stringFilterExpression,stringsort, dataviewrowstate recordstates)146 147         {148 149datarow[] Arraydr =dt. Select (FilterExpression, sort,recordstates); Max 151             foreach(DataRow Drincharraydr) the 153             {154 155Console.WriteLine (dr[0]. ToString () +"    "+ dr[1]. ToString ());156 157             }158 159 console.readline (); the 161         }162 163     }164 165}
View Code

Note: The select operation above is case insensitive (record fields are not sensitive) and if case sensitivity is required, the CaseSensitive property of the DataTable needs to be set to true.

Thank you: http://blog.csdn.net/lubiaopan/article/details/5880220

Introduction to the Select () method of the C # datatable

Related Article

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.