使用linq vb.net从可数据化中获取不同的值[英] Retrieve distinct values from datatable using linq vb.net

本文是小编为大家收集整理的关于使用linq vb.net从可数据化中获取不同的值的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

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

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

推荐答案

你只需要选择列并使用 Enumerable.Distinct:

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

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

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

本文地址:https://www.itbaoku.cn/post/1557105.html

问题描述

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

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

推荐答案

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()

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