none
problem with Distinct RRS feed

  • Question

  • This query returns a list that is neither ordered or distinct. Any help would be appreciated.

    Dim query = From c In context.ziptaxes Where c.StateID = ddlState.SelectedValue Order By c.City Select c.City, c.ZipTaxId
               
                ddlCity.DataSource = query.Distinct().ToList()
                ddlCity.DataValueField = "ziptaxid"
                ddlCity.DataTextField = "City"
                ddlCity.DataBind()


    Thomas Donino Visual Studio 2010 on Windows 7

    Wednesday, September 12, 2012 7:16 PM

All replies

  • Hi dinotom;

    If two rows have one field different from another they are NOT Distinct. So for example the folloing two lines are Distinct because ZipTaxId differ.

    City     ZipTaxId
    CityA    ID1
    CityA    ID2

    If you need to say that two lines are Distinct by only one field then you need to create a IEqualityComparer so that Distinct uses it. The following code sample show how to use City to make one line Distinct from another.

    Dim query = From c In context.ziptaxes 
                Where c.StateID = ddlState.SelectedValue 
                Order By c.City 
                Select New DDLInfo With
                       {
                           .City = c.City,
                           .ZipTaxId = c.ZipTaxId
                       }
    
    
    ddlCity.DataSource = query.ToList().Distinct(New CityEqualityComparer())
    
    ' Linq will create this Class to store the results
    Public Class DDLInfo
        Public City As String
        Public ZipTaxId As String
    End Class
    
    ' Used to compare two data items for Distinctness
    Public Class CityEqualityComparer
        Implements IEqualityComparer(Of DDLInfo)
    
        Public Overloads Function Equals(ByVal b1 As DDLInfo, ByVal b2 As DDLInfo) _
                   As Boolean Implements IEqualityComparer(Of DDLInfo).Equals
            If b1.City.Equals(b2.City) Then
                Return True
            Else
                Return False
            End If
        End Function
    
    
        Public Overloads Function GetHashCode(ByVal bx As DDLInfo) _
                    As Integer Implements IEqualityComparer(Of DDLInfo).GetHashCode
            Return bx.City.GetHashCode()
        End Function
    
    End Class

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, September 13, 2012 3:08 AM
  • The order by is not working either as the list returned is not sorted by any means. Is that syntax wrong as well?

    Thomas Donino Visual Studio 2010 on Windows 7

    Thursday, September 13, 2012 11:13 AM
  • Hi Thomas;

    I have tried a similar query on MS SQL server and it has correctly sorted it. So If you are using the exact query as you have posted it should be ordering it correctly.

    What database server are you using, MS SQL, Oracle, MySQL?

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, September 13, 2012 2:53 PM
  • Hi Dinotom,

    Any updates on this thread? What database server are you using?

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, September 19, 2012 7:16 AM