How to remove duplicate item "Go" in a DataTable

Source: Internet
Author: User

One of the problems in the project last week was to get the value of a column in the DataTable, since retrieving data from a database is grouped by 2 fields, and the column to be obtained is exactly in the two columns of the grouping, so the value of the column is bound to be duplicated, so I think of removing duplicates, With the idea after the online look at some of the methods, mostly traversal, although the function can be achieved, but the efficiency is too low, and finally found a simple method, as follows:

 public string[] Getnamesfromdatatable (DataTable DataTable)
        {
            DataView dv = Datatable.defaultview;
            dataTable = dv. ToTable (True, "Name");
            string[] names = new string[ DataTable.Rows.Count];
            for (int i = 0; i < names. Length; i++)
            {
                 Names[i] = datatable.rows[i][0]. ToString ();
           }
            return names;
       }

  

Parsing: The Dataview.totable () method has four overloaded methods, respectively, as follows:

ToTable (): Creates and returns a new DataTable based on the rows in the existing DataView

ToTable (string tableName): function as above, just to assign a name to the new DataTable

Todatatable (bool Distinct,parm string[] columnnames): Creates and returns DataView based on rows in existing datatable,distinct, true to return rows with different values for all columns , the second parameter is an array of characters, that is, you can specify the column to get, in the example above, only one column is specified, that is, to get the non-duplicated information for a column.

Todatatable (String Tablename,bool distinct,parm string[] columnnames); function as above, just one more datatablename

How to remove duplicate item "Go" in a 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.