PC Review


Reply
Thread Tools Rate Thread

Group By in DataTable?

 
 
Oka Morikawa
Guest
Posts: n/a
 
      22nd May 2004
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


 
Reply With Quote
 
 
 
 
Cor Ligthert
Guest
Posts: n/a
 
      22nd May 2004
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


 
Reply With Quote
 
Oka Morikawa
Guest
Posts: n/a
 
      22nd May 2004
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

"Cor Ligthert" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      22nd May 2004
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
///


 
Reply With Quote
 
Oka Morikawa
Guest
Posts: n/a
 
      22nd May 2004
Thanks, you code is quite nice

"Cor Ligthert" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
> ///
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete a group of records in a DataTable. Mufasa Microsoft ADO .NET 1 4th Dec 2008 08:12 AM
How group by and sum values in DataTable? Ronald S. Cook Microsoft C# .NET 2 13th May 2008 10:42 PM
DataTable.Select - Group By Doug Microsoft ADO .NET 1 11th May 2007 10:14 PM
what is the best way to make a DataTable thread safe? DataTable.AcceptChanges() will throw exception (not documented) Ryan Liu Microsoft C# .NET 3 7th Jun 2006 06:07 PM
Aggregate functions with GROUP BY Clauses on DataTable? =?Utf-8?B?Sm9iIExvdA==?= Microsoft Dot NET 1 2nd Aug 2004 08:18 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:15 AM.