Re: DataTable.Compute or .Select to count distinct values in a column

Discussion in 'Microsoft ADO .NET' started by Miha Markic [MVP C#], Aug 27, 2004.

  1. Hi,

    I would go with a simple foreach (DataRow row in table.Rows) loop.

    --
    Miha Markic [MVP C#] - RightHand .NET consulting & development
    miha at rthand com
    www.rthand.com

    "M K" <> wrote in message
    news:...
    >I have a Datatable in a DataSet that for one part of the process I need to
    > know the count of DISTINCT/UNIQUE values in one column. How can I achieve
    > this.
     
    Miha Markic [MVP C#], Aug 27, 2004
    #1
    1. Advertisements

  2. Miha Markic [MVP C#]

    Sasidhar Guest

    Miha,
    May be I am not getting you. How can you do that using a foreach? Can you
    please explain.

    MK,
    Look at this article. You can utilise it to find out the count.
    http://support.microsoft.com/default.aspx?scid=kb;EN-US;326176

    Or if the number of rows is less then you can use a for loop and a
    hashtable. As you loop insert into hashtable. If you find a duplicate ignore
    the key. At the end you can just get the count from hashtable.

    -SP

    "Miha Markic [MVP C#]" <miha at rthand com> wrote in message
    news:...
    > Hi,
    >
    > I would go with a simple foreach (DataRow row in table.Rows) loop.
    >
    > --
    > Miha Markic [MVP C#] - RightHand .NET consulting & development
    > miha at rthand com
    > www.rthand.com
    >
    > "M K" <> wrote in message
    > news:...
    > >I have a Datatable in a DataSet that for one part of the process I need

    to
    > > know the count of DISTINCT/UNIQUE values in one column. How can I

    achieve
    > > this.

    >
    >
     
    Sasidhar, Aug 27, 2004
    #2
    1. Advertisements

  3. Hi,

    "Sasidhar" <> wrote in message
    news:...
    > Miha,
    > May be I am not getting you. How can you do that using a foreach? Can you
    > please explain.


    Well, yes, doing it manually was what i was thinking.
    Storing unique value and its count (if you want to count the key occurences
    also) in a hashtable is a good one.

    --
    Miha Markic [MVP C#] - RightHand .NET consulting & development
    miha at rthand com
    www.rthand.com

    > MK,
    > Look at this article. You can utilise it to find out the count.
    > http://support.microsoft.com/default.aspx?scid=kb;EN-US;326176
    >
    > Or if the number of rows is less then you can use a for loop and a
    > hashtable. As you loop insert into hashtable. If you find a duplicate
    > ignore
    > the key. At the end you can just get the count from hashtable.
    >
    > -SP
    >
    > "Miha Markic [MVP C#]" <miha at rthand com> wrote in message
    > news:...
    >> Hi,
    >>
    >> I would go with a simple foreach (DataRow row in table.Rows) loop.
    >>
    >> --
    >> Miha Markic [MVP C#] - RightHand .NET consulting & development
    >> miha at rthand com
    >> www.rthand.com
    >>
    >> "M K" <> wrote in message
    >> news:...
    >> >I have a Datatable in a DataSet that for one part of the process I need

    > to
    >> > know the count of DISTINCT/UNIQUE values in one column. How can I

    > achieve
    >> > this.

    >>
    >>

    >
    >
     
    Miha Markic [MVP C#], Aug 27, 2004
    #3
  4. Miha Markic [MVP C#]

    Guest Guest

    Re: DataTable.Compute or .Select to count distinct values in a col

    What I would want is a count of how many different values there are. I could
    dump to a hash table, then get the .length. Seems costly...
     
    Guest, Aug 30, 2004
    #4
  5. Re: DataTable.Compute or .Select to count distinct values in a col

    Hi Mark,

    The .net framework's DataTable's select method only provide some very basic
    semantics, which haven't include the "unique item count" function, you can
    check all the supported semantics in the following link:

    #DataColumn.Expression Property
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
    frlrfsystemdatadatacolumnclassexpressiontopic.asp

    If you do want to do the unique item query, I think you should consider
    other members suggestion that manually loop through the items and count the
    distinct ones.


    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
     
    Steven Cheng[MSFT], Aug 31, 2004
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Ross

    DataTable.Select FilterExpression with Distinct

    Kevin Ross, Jul 31, 2003, in forum: Microsoft ADO .NET
    Replies:
    0
    Views:
    2,462
    Kevin Ross
    Jul 31, 2003
  2. Balu Smiles

    How to use DISTINCT in dataTable.select method

    Balu Smiles, Oct 10, 2003, in forum: Microsoft ADO .NET
    Replies:
    1
    Views:
    556
    Rajesh Patel
    Oct 10, 2003
  3. Guest

    Datatable's Select and Compute methods

    Guest, Sep 27, 2004, in forum: Microsoft ADO .NET
    Replies:
    7
    Views:
    1,047
    Steven Cheng[MSFT]
    Sep 28, 2004
  4. Guest
    Replies:
    4
    Views:
    2,305
    Vasco Veiga [MS]
    Dec 6, 2005
  5. Andrea Caldarone

    Implementing SELECT DISTINCT with DataTable.Select method

    Andrea Caldarone, Jan 30, 2007, in forum: Microsoft ADO .NET
    Replies:
    2
    Views:
    1,166
    Andrea Caldarone
    Jan 30, 2007
Loading...

Share This Page