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

  • Thread starter Miha Markic [MVP C#]
  • Start date
M

Miha Markic [MVP C#]

Hi,

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

Sasidhar

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.
 
M

Miha Markic [MVP C#]

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.
 
G

Guest

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...
 
S

Steven Cheng[MSFT]

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top