Search
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Forums Author
Thu, 23-Oct-2014 Authors
Cs181
10
All Time Authors
Jayakumars
4180
Karthikanbarasan
3455
Chvrsri
3140

Latest members | More ...


(Statistics delayed by 5 minutes)
 Forum Post

Selecting distinct value from DataTable

Raja
Posted by Raja under C# on 12/21/2009 11:58:17 AM | Views : 24645 | Status : [Member] | Replies : 6


How to select distinct column value from a DataTable?

Regards,
Raja, USA


Posted by: Poster on: 12/21/2009 [Member] Starter

Use following code snippet. This code snippet has been slightly modified from http://support.microsoft.com/default.aspx?scid=kb;en-us;326176#1 article.


/// <summary>

/// Selects the distinct from data table.
/// </summary>
/// <param name="TableName">Name of the table.</param>
/// <param name="SourceTable">The source table.</param>
/// <param name="FieldName">Name of the field.</param>
/// <returns></returns>
public static DataTable DistinctDataFromDataTable(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 });
}
}
return dt;
}

/// <summary>
/// Columns the equal.
/// </summary>
/// <param name="A">The A.</param>
/// <param name="B">The B.</param>
/// <returns></returns>
private static bool ColumnEqual(object A, object B)
{

// Compares two values to see if they are equal. Also compares DBNULL.Value.
// Note: If your DataTable contains object fields, then you must extend this
// function to handle them in a meaningful way if you intend to group on them.

if (A == DBNull.Value && B == DBNull.Value) // both are DBNull.Value
return true;
if (A == DBNull.Value || B == DBNull.Value) // only one is DBNull.Value
return false;
return (A.Equals(B)); // value type standard comparison
}


Thanks

Raja, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 12/22/2009 [Member] [Microsoft_MVP] [MVP] Silver

Just create A DataView object from DataTable..


Use

dt = dt.DefaultView.ToTable(true, param);


Where param will hold the Colimn Name for which you want to make distinct values.

Strange huh... Yes , There are lots of strange things in .NET. Distinct should be there with DataTable itself.

:)

Cheers.

www.abhisheksur.com

Raja, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 12/22/2009 [Member] [Microsoft_MVP] [MVP] Silver

@Poster

My friend, you have tried so hard to find this.

You should have taken the help of Linq if you want to implement this yourself.

:)

www.abhisheksur.com

Raja, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Poster on: 12/22/2009 [Member] Starter

@Abhi2434

Thanks but I am using .NET Framework 2.0 and I do not think LINQ is supported in this framework and C# version that is why I tried this one.

Thanks

Raja, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 12/23/2009 [Member] [Microsoft_MVP] [MVP] Silver

Still in .NET 2.0. Hey, I seriously think this is the time to move over 3.5 because, .NET 4.0 will be out very soon.

I hope you will find the little gem LINQ very interesting.

And also .NET 2008 IDE is also superb(far better than 2005 IDE). You should try.

Cheers.

www.abhisheksur.com

Raja, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response


Found interesting? Add this to: