all groups > dotnet ado.net > january 2007 >
You're in the

dotnet ado.net

group:

Implementing SELECT DISTINCT with DataTable.Select method


Implementing SELECT DISTINCT with DataTable.Select method Andrea Caldarone
1/30/2007 9:40:35 AM
dotnet ado.net:
Hi all,

I've a standard DataTable, is there a way to get an array of value like that
one I obtain in SQL Server suppying the SELECT DISTINCT [field] statement?
Have I to loop trough the datatable to build the array or is there a simpler
way?

Re: Implementing SELECT DISTINCT with DataTable.Select method Miha Markic [MVP C#]
1/30/2007 9:47:07 AM
Hi Andrea,

No disctinct functionality there.
Loop is your best friend.
Also there is a 3rd party tool advertised in this newsgroup that does some
sql functionality over datasets (I keep forgotting its name - i have to
write it down).
--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

[quoted text, click to view]
Re: Implementing SELECT DISTINCT with DataTable.Select method Andrea Caldarone
1/30/2007 10:28:02 AM
[quoted text, click to view]

I've implemented a simple function that make the SELECT DISTINCT for me,
have a look:

Public Function SelectDistinct(ByRef tX As DataTable, ByVal strField As
String) As IList

Dim rQuery() As DataRow
Dim lRET As New Collection
Dim i As Integer
Dim j As Integer

'Sort the DataTable by the Field
rQuery = tX.Select("", strField & " ASC")

Select Case rQuery.GetUpperBound(0)
Case Is = -1

'no rows
Exit Select
Case Is = 0

'1 row
lRET.Add(CType(rQuery.GetValue(0), DataRow).Item(strField))
Case Else

'more than 1 row
lRET.Add(CType(rQuery.GetValue(0), DataRow).Item(strField))
j = 1

For i = 1 To rQuery.GetUpperBound(0)
If rQuery(i).Item(strField) <> lRET.Item(j) Then
lRET.Add(CType(rQuery.GetValue(i),
DataRow).Item(strField))
j += 1
End If
Next
End Select

'function return
Return lRET
End Function

AddThis Social Bookmark Button