Group By in DataTable?

O

Oka Morikawa

How do I accomplish this kind of T-SQL in DataTable?
SELECT SizeW
FROM dbo.Product
GROUP BY SizeW

I need to get a list of used widths on Product table and I could to it with
IF row by row but is this possible to make with somehow
DataTable.Column.Expression or someway on DataTable.Compute?


Thanks!
Oka Morikawa
 
C

Cor Ligthert

Hi Oka,

I think that for this problem, you can use the dataview.rowfilter
(which I prefer because it is the fastest and nicest method)

Or you can use the datatable.select

Both use an expression as filter

I hope this helps?

Cor
 
O

Oka Morikawa

Hi,
I can't use those for this problem.
I have orders where products have different sizes but also there can be
duplicate sizes on same order.
eg.

Order with products with following width:
1. 105
2. 120
3. 120
4. 90
5. 105

....and trying to get following result:
1. 90
2. 105
3. 120

It's so easy to do with T-SQL on SqlServer but since I this is disconnected
app I need to get that result somehow by ado.net.

oka
 
C

Cor Ligthert

Hi Oka,

I was thinking that that was the problem, however was not sure in the way
you did describe it.

There is not a distinct as a class in dotNet.

You can try this sample I once made as a generic class for it.

This is VB, when you need it in C# use it as a template, there are no
special VB functions in it only the standard dotNet subset.

I hope this helps?

Cor
\\\\
' somewhere in your program
e.DataGrid1.DataSource = mydistinct.distinct(dt, "elem1")
End Sub
End Class
\\\
Public Class Selectclass
Public Function distinct(ByVal dt As DataTable, _
ByVal dist As String) As DataTable
Dim dtclone As DataTable = dt.Clone
Dim dv As New DataView(dt)
dv.Sort = dist
Dim myselold As String = ""
For i As Integer = 0 To dv.Count - 1
If myselold <> dv(i)(dist).ToString Then
Dim drn As DataRow = dtclone.NewRow
For y As Integer = 0 To dv.Count - 1
drn(y) = dv(i)(y)
Next
myselold = dv(i)(dist).ToString
dtclone.Rows.Add(drn)
End If
Next
Return dtclone
End Function
End Class
///
 

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