简体   繁体   中英

Remove Duplicates Linq Query Dropdownlist in VB.net

I have bound my data source to the drop down list and it works, but it is showing duplicates. Here is my code

Private Sub ddlMasterPids_Load(sender As Object, e As EventArgs) Handles ddlMasterPids.Load
    Dim db As New DesignConstructionDataContext

    Dim Master = (From Master_Name In db.groups
                  Where (Master_Name.Master_Name IsNot Nothing)
                  Select Master_Name).ToList().Distinct()

    ddlMasterPids.DataSource = Master
    ddlMasterPids.DataTextField = "Master_Name"
    ddlMasterPids.DataValueField = "Master_Name"
    ddlMasterPids.DataBind()
End Sub

The .Distinct() does not throw an error, but there are still duplicates. I also tried switching the distinc and tolist, but still just ignored the distinct. Any ideas?

You are not selecting the column Master_Name but the row in the table Master_Name . That why Distinct doesn't work as expected. The rows have at least one column that is different.

Instead you want this:

Dim Master = (From Master_Name In db.groups
              Where Master_Name.Master_Name IsNot Nothing
              Select Master_Name.Master_Name).Distinct().ToList()

Note also that i call Distinct before ToList to filter already in the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM