DataTable.Compute() question

S

SpaceMarine

hello,

i have a big datable of a bunch of stuff. i am interested in getting
row counts that match certain criteria.

currently i do this by creating a new DataView for each criteria, and
getting its .RowCount. like so:

dv = New DataView(dt, "Type = 2", "Type",
DataViewRowState.CurrentRows)
total = dv.Count
writer.WriteLine(String.Format("Lines of Type 2: {0}.", total))

....that works, but i was wondering -- is there a way to use the
DataTable.Compute() method to do this? i had tried something like
this, but it didnt work:

Dim oTotal As Object = dt.Compute("Count(*)", "Type = 2")

....this doesnt work because the aggregate function Count() isnt
designed to work this way. but is there something that is?

thanks!
matt
 
C

Cor Ligthert[MVP]

Matt,

Maybe there is, however what you think to win with that, in my idea is your
method very inventive and most probably the fastest you can get.

Cor
 
M

Miha Markic

No, you can't use Compute for this one.
As an alternative (similar to your solution) you might use DataTable.Select
method.
 
A

Amit

hello,

i have a big datable of a bunch of stuff. i am interested in getting
row counts that match certain criteria.

currently i do this by creating a new DataView for each criteria, and
getting its .RowCount. like so:

dv = New DataView(dt, "Type = 2", "Type",
DataViewRowState.CurrentRows)
total = dv.Count
writer.WriteLine(String.Format("Lines of Type 2: {0}.", total))

...that works, but i was wondering -- is there a way to use the
DataTable.Compute() method to do this? i had tried something like
this, but it didnt work:

Dim oTotal As Object = dt.Compute("Count(*)", "Type = 2")

...this doesnt work because the aggregate function Count() isnt
designed to work this way. but is there something that is?

thanks!
matt

Can't you use dt.Rows.Length

And I don't think DataTable.Select provides too much options either
 
M

Miha Markic

Amit said:
Can't you use dt.Rows.Length

No. He needs to count *filtered* rows, not all rows.
And I don't think DataTable.Select provides too much options either

No, it doesn't - it is quite same as creating a DataView, it is just less
code in this scenario.[/QUOTE]
 

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