vb.net LINQ选择“与列表不同" [英] vb.net LINQ select Distinct to a List

查看:117
本文介绍了vb.net LINQ选择“与列表不同"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datatable,其列中有一些重复的值,我想将这些值添加到listbox中,但不能重复

I have a datatable with a column that has some duplicate values, I want to add those values to a listbox but without duplicates

我尝试了以下

Dim a = From row In table.AsEnumerable.Distinct.ToList Select row.Field(Of String)("name")

但是它给了我重复的值,没有重复该怎么办?

but it gives me duplicate values, How can it be done without duplicates?

推荐答案

我相信每一行中都有更多唯一的列,这就是为什么distinct无法返回预期结果的原因.相反,您应该先选择列,然后再对它们应用不重复列.

I believe there are some more column(s) which are unique in each row that's why the distinct doesn't return the result as expected. Instead you should need to select the columns first than apply the distinct to it.

所以请尝试以下方法:

Dim a = (From row In table.AsEnumerable()
        Select row.Field(Of String)("name")).Distinct().ToList()

希望这会有所帮助!

这篇关于vb.net LINQ选择“与列表不同"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆