简体   繁体   English

在VB.net中删除重复的Linq查询下拉列表

[英]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. .Distinct()不会引发错误,但是仍然存在重复项。 I also tried switching the distinc and tolist, but still just ignored the distinct. 我也尝试过切换distinc和tolist,但仍然忽略了区别。 Any ideas? 有任何想法吗?

You are not selecting the column Master_Name but the row in the table Master_Name . 您不是在选择Master_Name列,而是在表Master_Name选择行。 That why Distinct doesn't work as expected. 这就是为什么Distinct无法按预期工作的原因。 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. 还要注意,我在ToList之前调用Distinct来过滤数据库中已经存在的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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