Remove repeated fields in datatable

Source: Internet
Author: User

 

# Region Remove repeated fields in datatable
///   <Summary>
///   </Summary>
///   <Param name = "sourcetable"> </param>
///   <Param name = "fieldnames"> </param>
///   <Returns> </returns>
Protected Datatable selectdistinct (datatable sourcetable, Params String [] Fieldnames)
{
Object [] Lastvalues;
Datatable newtable;
Datarow [] orderedrows;

//If (fieldnames = NULL | fieldnames. Length = 0)
//Throw new argumentnullexception ("fieldnames ");

// If fieldnames is empty, all columns are queried by default.
If (Fieldnames =Null | Fieldnames. Length = 0 )
{
Fieldnames = New String [Sourcetable. Columns. Count];
For ( Int I =0 ; I <sourcetable. Columns. Count; I ++)
{
Fieldnames [I] = sourcetable. Columns [I]. columnname;
}
}

Lastvalues = New Object [Fieldnames. Length];
Newtable = New Datatable ();

Foreach(StringFieldnameInFieldnames)
Newtable. Columns. Add (fieldname, sourcetable. Columns [fieldname]. datatype );

orderedrows = sourcetable. select ( "" , string . join ( " , " , fieldnames ));

foreach (datarow row in orderedrows)
{< br> If (! Fieldvaluesareequal (lastvalues, row, fieldnames)
{< br> newtable. Rows. Add (createrowclone (row, newtable. newrow (), fieldnames);

Setlastvalues (lastvalues, row, fieldnames );
}
}
Return Newtable;
}
Private Bool Fieldvaluesareequal (Object [] Lastvalues, datarow currentrow, String [] Fieldnames)
{
Bool Areequal = True ;

For ( Int I = 0 ; I <fieldnames. length; I ++)
{
If (Lastvalues [I] = Null |! Lastvalues [I]. Equals (currentrow [fieldnames [I])
{
Areequal = False ;
Break ;
}
}

return areequal;
}< br> private datarow createrowclone (datarow sourcerow, datarow newrow, string [] fieldnames)
{< br> string Field in fieldnames)
newrow [field] = sourcerow [field];

Return Newrow;
}
Private Void Setlastvalues ( Object [] Lastvalues, datarow sourcerow, String [] Fieldnames)
{
For ( Int I = 0 ; I <fieldnames. length; I ++)
Lastvalues [I] = sourcerow [fieldnames [I];
}
# Endregion

 

 

 

The call is as follows:

Datatable TBL = new datatable ();
TBL. Columns. Add ("ID", typeof (system. int32 ));
TBL. Columns. Add ("city", typeof (system. String ));
TBL. Columns. Add ("Province", typeof (system. String ));

TBL. Rows. Add (1, "Wuhan", "Hubei ");
TBL. Rows. Add (2, "Yingcheng", "Hubei ");
TBL. Rows. Add (3, "Wuhan", "Hubei ");

// datatable dt2 = selectdistinct (TBL, null);
datatable dt2 = selectdistinct (TBL, "city", "Province ");
// datatable dt2 = selectdistinct (TBL, new string [] {"city", "Province"});
This. datagridview1.datasource = dt2;

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.