SIGN UP MEMBER LOGIN:    
ARTICLE

How to: Select distinct values from Dataset

Posted by anand palanichamy Articles | ADO.NET in C# June 07, 2010
In this article you will see how to get the distinct values from the Dataset.
Reader Level:

Dataset is a most powerful and easy-to-use component of ADO.Net. In this section you will see how to get the distinct values from the Dataset.

CODE:

#region DATASET HELPER
private bool ColumnEqual(object A, object B)
{
    // Compares two values to see if they are equal. Also compares DBNULL.Value.           
    if (A == DBNull.Value && B == DBNull.Value) //  both are DBNull.Value
            return true;
    if (A == DBNull.Value || B == DBNull.Value) //  only one is BNull.Value
            return false;
    return (A.Equals(B));  // value type standard comparison
}
public DataTable SelectDistinct(DataTable SourceTable, string FieldName)
{
    // Create a Datatable – datatype same as FieldName
    DataTable dt = new DataTable(SourceTable.TableName);
    dt.Columns.Add(FieldName, SourceTable.Columns[FieldName].DataType);
    // Loop each row & compare each value with one another
    // Add it to datatable if the values are mismatch
    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;
}
#endregion

Example:

Consider the following Dataset dsOrders,

1.gif

Get distinct Product from this Dataset, 

DataTable distinctDT = SelectDistinct(dsOrders.Tables[0], "Product");

For CashMode,

DataTable distinctDT = SelectDistinct(dsOrders.Tables[0], "CashMode");

Output:

2.gif

Login to add your contents and source code to this article
share this article :
post comment
 

You can select distinct from dataset as datatable dt= dsOrders.Tables[0]..DefaultView.ToTable(True, "ColumnName")

Posted by pooja shete Jun 28, 2011

Goode one

Posted by sunil t Jun 07, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor