Datatable. Select distinct

Source: Internet
Author: User
Code
  ///   <Summary>
/// Returns the datatable after the select distinct operation.
///   </Summary>
///   <Param name = "sourcetable"> Source data table </Param>
///   <Param name = "fieldnames"> Field set </Param>
///   <Returns> </returns>
Private Datatable selectdistinct (datatable sourcetable, Params   String [] Fieldnames)
{
Object [] Lastvalues;
Datatable newtable;
Datarow [] orderedrows;

If (Fieldnames =   Null   | Fieldnames. Length =   0 )
Throw   New Argumentnullexception ( " Fieldnames " );

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

foreach ( string fieldname in fieldnames)
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 );
}
}

ReturnNewtable;
}

Private BoolFieldvaluesareequal (Object[] Lastvalues, datarow currentrow,String[] Fieldnames)
{
BoolAreequal= True;

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

ReturnAreequal;
}

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

ReturnNewrow;
}

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

 

 

Datatable. Select

 

Public datatable filltable (string m_where, datatable m_table)
{
Datatable _ temp = m_table.clone ();
Datarow [] _ ROW = m_table.select (m_where );

Foreach (datarow _ r in _ row)
{

_ Temp. Rows. Add (_ R. itemarray );
}
Return _ temp;
}

 

Viewstate ["backurl"] = request. urlreferrer. tostring ();

 

protected void button1_click1 (Object sender, eventargs e)
{< br> If (viewstate ["backurl"]! = NULL)
{< br> response. Redirect (viewstate ["backurl"]. tostring ();
}< BR >}

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.