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

  • Thread starter Thread starter Miha Markic [MVP C#]
  • Start date Start date
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 said:
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 said:
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.
 
Hi,

Sasidhar said:
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 said:
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 said:
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.
 
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...
 
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
 
Back
Top