Question : Linq to DataTable and getting Distinct rows

The following Linq query is not returning distinct rows

Dim odt As DataTable = CType(Session("GridData"), DataTable)
'Contains Email addresses (and other data columns) the column name is 'Email'
'[email protected]'
'[email protected]'
'[email protected]'
'[email protected]'

'Define linq query
Dim lQuery = From c In odt.AsEnumerable() _
                     Where c!Email IsNot DBNull.Value _
                     Order By c!Email Ascending _
                     Select New With {.Email = c.Field(Of String)("Email")}

'Populate Chkboxlist
chklRecipients.DataSource = lQuery.Distinct()
chklRecipients.DataBind()

This is not removing duplicate row '[email protected]' because it is doing a binary compare and not a string compare. What do I need to do to stop these duplicates and the simpler the better?

Answer : Linq to DataTable and getting Distinct rows

I adjusted your query
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
Dim x As New DataTable()
        Dim col1 As New DataColumn("email")

        x.Columns.Add(col1)

        x.Rows.Add("[email protected]")
        x.Rows.Add("[email protected]")
        x.Rows.Add("[email protected]")
        x.Rows.Add("[email protected]")



        Dim query = (From q In (From c In odt.AsEnumerable() _
                                Where c!Email IsNot DBNull.Value _
                                Order By c!Email Ascending _
                                Select New With {.mail = c("Email")}) _
                     Select q.mail Distinct)
Random Solutions  
 
programming4us programming4us