Click here to Skip to main content
Licence CPOL
First Posted 14 Nov 2006
Views 30,246
Bookmarked 16 times

Distinct IDs from DataSet

By | 14 Nov 2006 | Article
An article on selecting distinct values from a DataSet.
 
Part of The SQL Zone sponsored by
See Also

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.

/// <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)

About the Author

Mahendra Kumar Srivastava

Software Developer (Senior)
Misys
India India

Member

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 with Misys in Banking domain.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
AnswerSELECT DISTINCT FROM DataTable in VB.NET PinmemberHenry Thet21:55 19 Sep '10  
Generalthis might also help some of you PinmemberKaleemullah khan21:21 22 Oct '07  
QuestionHow to use the code PinmemberMahendra Kumar Srivastava10:24 7 Dec '06  
GeneralDataView.ToTable Method (String, Boolean, String[]) PinmemberIzhar A.21:59 24 Nov '06  
From:
ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref4/html/M_System_Data_DataView_ToTable_1_5f4fb82d.htm
 
Use this overloaded version of the ToTable method if you have to retrieve distinct values in a subset of available columns, specifying a new name for the returned DataTable. If you do not need distinct rows or a subset of columns, see ToTable.

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 14 Nov 2006
Article Copyright 2006 by Mahendra Kumar Srivastava
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid