1

I'm using vb.net framework 3.5

I have a dataset name dsData which contains the next info

Page        User     Permission
Simulator   agarza   1
Buys        agarza   1
File loads  agarza   0
scenaries   agarza   0
Simulator   mjobs    1
Buys        mjobs    0
File loads  mjobs    1
scenaries   mjobs    1

i want to obtain the distinct names of users, I'm trying to do something like this:

    Dim query= From row In dsData.Tables(0).AsEnumerable() _
               Select row.Field(Of String)("User") Distinct

in the previous code i get this error: Definition of method 'Distinct' is not accessible in this context.

and then I would like to have the result in a datatable, doing something like this

    Dim dt As DataTable = query.CopyToDataTable()

someone can help me?...

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
AaronDev
  • 13
  • 1
  • 1
  • 4
  • If Distinct isnt avaible that indicates the object you try to use it on doesnt implement EqualityComparer. My Wild guess is, you need to call row.Field(OfString)("User").Value or something ?! Im not into VB though so it might be total bs what i wrote here. – CSharpie Apr 11 '13 at 18:18

3 Answers3

5

You can use ToTable(distinct As Boolean, ParamArray columnNames As String()) method for this.

dsData.Tables(0).DefaultView.ToTable(True, "User")

This will return distinct Users for you. You can add multiple column names if you want.

Here is the msdn documentation. https://msdn.microsoft.com/en-us/library/wec2b2e6(v=vs.110).aspx

Chinthaka
  • 343
  • 3
  • 13
  • 1
    Could you please [edit] in an explanation of why this code answers the question? Code-only answers are [discouraged](http://meta.stackexchange.com/q/148272/274165), because they don't teach the solution. – Nathan Tuggy Sep 29 '15 at 20:08
3

The syntax is

(From row In dsData.Tables(0).AsEnumerable() _
           Select row.Field(Of String)("User")).Distinct()

As for copying to data table, check out the answer to Exception using CopyToDataTable with "new {..}" LINQ query.

Community
  • 1
  • 1
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
2

Try using method based query syntax:

Dim names As List(Of String) = dsData.Tables(0).AsEnumerable() _
                                               .Select(Function(r) r.Field(Of String)("User")) _
                                               .Distinct() _
                                               .ToList()
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263