使用linq vb.net从数据表中检索不同的值 [英] Retrieve distinct values from datatable using linq vb.net

查看:68
本文介绍了使用linq vb.net从数据表中检索不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据表中的特定列检索所有不同的值.数据表中的列名称为计数".我有2240行,计数"列中有6个不同的值.问题是,当我执行以下代码时,它给出的是行数,而不是6个不同的值.

I am trying to retrieve all of the distinct values from a particular column in a datatable. The column name in the datatable is "Count". I have 2240 rows and I have 6 distinct values in the "Count" column. The problem is, when I execute the following code, it is giving me the number of rows rather than the 6 distinct values.

Dim counts = (From row In loadedData
Select row.Item("Count")).Distinct()
For Each i In counts
    MsgBox(i)
Next

如何修改它以检索6个不同的值,而不是给我总的行数?

How can I modify this to retrieve the 6 distinct values, rather than it giving me the total number of rows?

推荐答案

您只需选择该列并使用Enumerable.Distinct:

You just have to select the column and use Enumerable.Distinct:

Dim distinctCounts As IEnumerable(Of Int32) = loadedData.AsEnumerable().
    Select(Function(row) row.Field(Of Int32)("Count")).
    Distinct()

在查询语法中(我什至不知道VB.NET甚至直接支持Distinct):

In query syntax(i didn't know that even Distinct is supported directly in VB.NET):

distinctCounts = From row In loadedData
                 Select row.Field(Of Int32)("Count")
                 Distinct

这篇关于使用linq vb.net从数据表中检索不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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