[Csharp] DataView DV = dtLineTypeURL. DefaultView;
DataTable dtTmp = DV. ToTable ("tb1", true, "LineType"); // use distinct for LineType
DtLineTypeURL = dtTmp;
DataView DV = dtLineTypeURL. DefaultView;
DataTable dtTmp = DV. ToTable ("tb1", true, "LineType"); // use distinct for LineType
DtLineTypeURL = dtTmp; or
[Csharp] dtLineTypeURL = dtLineTypeURL. DefaultView. ToTable (true, "LineType"); // use distinct for LineType
DtLineTypeURL = dtLineTypeURL. DefaultView. ToTable (true, "LineType"); // use distinct for LineType
Note: The dataTable obtained in the preceding method only has the LineType column, and other columns are discarded.
If you only want to perform distinct on a column and keep other columns, use the following:
[Csharp] # region deletes duplicate columns of a able, similar to distinct
/// <Summary>
/// Delete the repeated DataTable columns, similar to distinct
/// </Summary>
/// <Param name = "dt"> DataTable </param>
/// <Param name = "Field"> Field name </param>
/// <Returns> </returns>
Public static DataTable DeleteSameRow (DataTable dt, string Field)
{
ArrayList indexList = new ArrayList ();
// Locate the row index to be deleted
For (int I = 0; I <dt. Rows. Count-1; I ++)
{
If (! IndexList. Contains (I ))
{
For (int j = I + 1; j <dt. Rows. Count; j ++)
{
If (dt. Rows [I] [Field]. ToString () = dt. Rows [j] [Field]. ToString ())
{
IndexList. Add (j );
}
}
}
}
IndexList. Sort ();
// Delete a row based on the index list to be deleted
For (int I = indexList. Count-1; I> = 0; I --)
{
Int index = Convert. ToInt32 (indexList [I]);
Dt. Rows. RemoveAt (index );
}
Return dt;
}
# Endregion
# Region Delete the repeated DataTable columns, similar to distinct
/// <Summary>
/// Delete the repeated DataTable columns, similar to distinct
/// </Summary>
/// <Param name = "dt"> DataTable </param>
/// <Param name = "Field"> Field name </param>
/// <Returns> </returns>
Public static DataTable DeleteSameRow (DataTable dt, string Field)
{
ArrayList indexList = new ArrayList ();
// Locate the row index to be deleted
For (int I = 0; I <dt. Rows. Count-1; I ++)
{
If (! IndexList. Contains (I ))
{
For (int j = I + 1; j <dt. Rows. Count; j ++)
{
If (dt. Rows [I] [Field]. ToString () = dt. Rows [j] [Field]. ToString ())
{
IndexList. Add (j );
}
}
}
}
IndexList. Sort ();
// Delete a row based on the index list to be deleted
For (int I = indexList. Count-1; I> = 0; I --)
{
Int index = Convert. ToInt32 (indexList [I]);
Dt. Rows. RemoveAt (index );
}
Return dt;
}
# Endregion
Call method:
DtLineTypeURL = Common. DeleteSameRow (dtLineTypeURL, "LineType"); // use distinct for LineType