ASP. NET: implement a dataset or datatable select distinct (field uniqueness)

Source: Internet
Author: User

 

// This method copies the unique value of the selected field to a new able. If the field contains null values, the records in the target table also contain null values.
Public Datatable selectdistinct ( String Tablename, datatable sourcetable, String Fieldname)
{
Datatable dt =   New Datatable (tablename );
DT. Columns. Add (fieldname, sourcetable. Columns [fieldname]. datatype );

Object Lastvalue =   Null ;
Foreach (Datarow Dr In Sourcetable. Select ( "" , Fieldname ))
{
If (Lastvalue =   Null   |   ! (Columnequal (lastvalue, Dr [fieldname])
{
Lastvalue = Dr [fieldname];
DT. Rows. Add ( New   Object [] {Lastvalue });
}
}
/// /If (Ds! = NULL)
/// /Ds. Tables. Add (DT );
Return DT;
}
Static   Bool Columnequal ( Object A, Object B)
{
If ( = Dbnull. Value && B = Dbnull. value) // Both are dbnull. Value
Return   True ;
If ( = Dbnull. Value | B = Dbnull. value) // Only one is dbnull. Value
Return   False ;
Return (A. Equals (B )); // Value Type Standard Comparison
}

 

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.