Click here to Skip to main content
15,885,014 members
Articles / Programming Languages / C#
Article

Distinct IDs from DataSet

Rate me:
Please Sign up or sign in to vote.
2.11/5 (8 votes)
14 Nov 2006CPOL 46.7K   16   4
An article on selecting distinct values from a DataSet.

Introduction

Sometimes we need to find out the distinct values in a row in a DataSet but ADO.NET doesn't provide any Select or other functions to do this. So we can implement a similar kind of functionality using the code snippet given in this article.

Background

I was working on a project and found that we can't get distinct IDs from a DataSet, which was required in some cases. So I have written a code snippet which may help you.

Using the code

The code is self explanatory, and contains comments.

C#
/// <summary>
/// Gives the uniqueId list from the rows
/// of the table which satisfy the given condition.
/// </summary>
/// <param name="dtSource">The source table from
/// which rows will be selected.</param>
/// <param name="IDColumnName">The ID column name
/// which will be created as list.</param>
/// <param name="Condition">The condition
/// based upon which rows will be selected.
/// If one wants to check all rows pass Condition as ""</param>
/// <returns>List of unique ids as string list
/// which satisfy the given condition</returns>
public List<string> GetDistinctIDs(DataTable dtSource, 
                       string IDColumnName, string Condition)
{
    List<string> arUniqueId = new List<string>();
    foreach (DataRow dr in dtSource.Select(Condition))
    {
        if(dr[IDColumnName] == DBNull.Value || 
           arUniqueId.Contains(dr[IDColumnName].ToString()))
        {
            continue;
        }
        arUniqueId.Add(dr[IDColumnName].ToString());
    }
    return arUniqueId;
}


/// <summary>
/// Gives the unique value rows from the rows
/// of the table which satisfy the given condition.
/// </summary>
/// <param name="dtSource">The source table
/// from which rows will be selected.</param>
/// <param name="IDColumnName">The ID column
/// name which will be created as list.</param>
/// <param name="ValueColumnName">The Value column name which
/// will be created as Value in Hashtable.</param>
/// <param name="Condition">The condition based
/// upon which rows will be selected. 
/// If one wants to select all rows pass Condition as ""
/// </param>
/// <returns>A Hashtable Containing unique keys
/// as Ids and Value corresponds to them.</returns>
public Hashtable GetDistinctValues(DataTable dtSource, string IDColumnName, 
                                   string ValueColumnName, string Condition)
{
    Hashtable ht = new Hashtable();
    foreach (DataRow dr in dtSource.Select(Condition))
    {
        if (dr[IDColumnName] == DBNull.Value || 
            ht.ContainsKey(dr[IDColumnName]))
        {
            continue;
        }
        ht.Add(dr[IDColumnName],dr[ValueColumnName]);
    }
    return ht;
}

Points of Interest

You can use this code snippet in many ways. The hash table is one way of implementation. If you want to implement multi-column values, then probably you can create a simple table instead of the Hashtable and add the rows and fill the values in the DataTable.

History

  • Ver. 1.0 by Mahendra.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader M*Modal
India India
Started carreer with CEERI, Delhi, India as a C Programmer in 2004 and migrated my learning experiance to C#.Net in 2005 with DIGISIGN Noida, Joined Bally in 2006, worked with JK Technosoft since Jun 2007 till Apr 2009, joined back Bally in June 2009 and joined Misys in June 2010. Worked in different domains such as Casino Management, Digital Signage, and Medical and currently working M*Modal as Lead - Product Development.

Comments and Discussions

 
AnswerSELECT DISTINCT FROM DataTable in VB.NET Pin
Henry Thet19-Sep-10 21:55
Henry Thet19-Sep-10 21:55 
Generalthis might also help some of you Pin
Kaleemullah khan22-Oct-07 21:21
Kaleemullah khan22-Oct-07 21:21 
QuestionHow to use the code Pin
Mahendra Kumar Srivastava7-Dec-06 10:24
Mahendra Kumar Srivastava7-Dec-06 10:24 
GeneralDataView.ToTable Method (String, Boolean, String[]) Pin
Izhar A.24-Nov-06 21:59
Izhar A.24-Nov-06 21:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.