使用 linq vb.net 从数据表中检索不同的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19247504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:13:01  来源:igfitidea点击:

Retrieve distinct values from datatable using linq vb.net

vb.netlinqdatatable

提问by John Janssen

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.

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

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?

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

回答by Tim Schmelter

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

您只需要选择列并使用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 Distinctis supported directly in VB.NET):

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

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

回答by Chinthaka

You can use ToTable(distinct As Boolean, ParamArray columnNames As String()) method for this.

您可以为此使用 ToTable(distinct As Boolean, ParamArray columnNames As String()) 方法。

loadedData.DefaultView.ToTable(True, "Count")

This will return distinct Users for you. You can add multiple column names if you want.

这将为您返回不同的用户。如果需要,您可以添加多个列名。

Here is the msdn documentation. https://msdn.microsoft.com/en-us/library/wec2b2e6(v=vs.110).aspx

这是 msdn 文档。 https://msdn.microsoft.com/en-us/library/wec2b2e6(v=vs.110).aspx

回答by Dhaval

you can also apply this logic if you want , first sort datatable through columName , then apply this logic

如果需要,您也可以应用此逻辑,首先通过 columName 对数据表进行排序,然后应用此逻辑

    dtRecords.DefaultView.Sort = "columnName"
    dtRecords = dtRecords.DefaultView.ToTable
    Dim totalRecords As Integer = 0
    Dim thNameStr As String = filter(dtRecords, "columnName", totalRecords )

Public Shared Function filter(ByVal dtRecords As DataTable, ByVal columnName As String, ByRef totalRecords As Integer) As String
            Dim FilterStr As String = ""
            Dim eachTotal As Integer = 0
            totalRecords = 0
            Dim lastName As String = ""
            For rCount = 0 To dtRecords.Rows.Count - 1
                If lastName <> "" And lastName <> dtRecords.Rows(rCount)("" & columnName) Then
                    FilterStr &= lastName & " - [" & eachTotal & "]"
                    eachTotal = 0
                    totalRecords += 1
                End If
                lastName = dtRecords.Rows(rCount)("" & columnName)
                eachTotal += 1
            Next
            FilterStr &= lastName & " - [" & eachTotal & "]"
            totalRecords += 1
            Return FilterStr 
        End Function