Querying in DataTable

C

Curious

Hi,

I have a DataTable with three columns (2 int, and 1 string). Now I
would like a query that return the total number of different integers
in one of the interger columns.

For example
Name Age Class
A 12 4
B 13 5
C 11 3
D 12 3


Now if I select the total number of different integers in the Age
column the answer is 3(because 11, 12 ,13).

Is this possible to do?
Can someone help me out
Thanks in Advance
 
T

The Crow

sql query or in program?

sql query is like:

SELECT COUNT(DISTINCT(age))
FROM Table


if you want to do that in program, i dont think there is such a function in
DataTable class. so you can try looping through all rows manually and find
by yourself
 
C

Curious

I'm to use it from a DataTable. Is that sort of query available in
someway using xml?, because the data is also available in an xml file.
 
C

Curious

Using the following code I succeded to count the number of rows, but
not required result.


XmlTextReader reader = new
XmlTextReader(File.OpenText("c:\\test.xml"));
XPathDocument doc = new XPathDocument(reader, XmlSpace.Preserve);
XPathNavigator nav = doc.CreateNavigator();

XPathExpression exp = nav.Compile("count(//Persons/Person/Age)");

Console.WriteLine(nav.Evaluate(exp).ToString());

This return 4, not 3. What further modifications can be done?
 
T

The Crow

i think yes. but i dont remember how. a xpath query will probably will be
able to do that.
 
C

Curious

If found this code on the net, but the following Result is being
returned: 'System.Xml.XPath.XPathSelectionIterator'.

What should my problem be?

XmlTextReader reader = new
XmlTextReader(File.OpenText("c:\\test.xml"));
XPathDocument doc = new XPathDocument(reader, XmlSpace.Preserve);
XPathNavigator nav = doc.CreateNavigator();

XPathExpression exp =
nav.Compile("//Statistics/ProcessState[not(ProcessID =
preceding-sibling::processState/ProcessID)]/ProcessID");

Console.WriteLine(nav.Evaluate(exp));
 
W

Wolfgang Schwedhelm

Hi Curious!

Try this:
SELECT COUNT(DISTINCT Age) AS AgeCount FROM Table1

Wolfgang
 

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