在 VB.Net 中过滤数据表

发布于 2024-12-06 17:12:52 字数 583 浏览 0 评论 0原文

我有一个具有以下结构的数据表。

mid    mname      version  baseID
------------------------------------
1      tag3         1       1
2      tag3         2       1
3      tag3         3       1
4      tag5         1       4
5      tag5         2       4 
6      tag6         1       6

从上面的结构我需要一个类似这样的数据表。 我需要从数据表中获取最高版本。

mid    mname      version  baseID
------------------------------------
3      tag3         3       1
5      tag5         2       4 
6      tag6         1       6

如何创建与上面类似的数据表。 我正在使用 Vb.Net

提前致谢。

I am having a datatable with following structure.

mid    mname      version  baseID
------------------------------------
1      tag3         1       1
2      tag3         2       1
3      tag3         3       1
4      tag5         1       4
5      tag5         2       4 
6      tag6         1       6

From the above structure I need a datatable similar like this.
I need to take the highest version from the datatable.

mid    mname      version  baseID
------------------------------------
3      tag3         3       1
5      tag5         2       4 
6      tag6         1       6

How can I create a datatable similar like the above.
I am using Vb.Net

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画尸师 2024-12-13 17:12:52

这取决于您如何填充原始数据表。如果您通过查询数据库来完成此操作,我会在查询本身中格式化结果集,如下所示:

(SQL)

select mid, mname, version, baseID
from <table> t
Join (select baseid, max(version) as version 
         from <table> group by baseID)maxVersion 
    on maxVersion.baseid = t.baseid and maxVersion.version = t.version

这将在将数据放入数据表之前格式化数据。

如果数据表是通过其他方式填充的,并且您需要对其进行过滤,然后将其放入另一个数据表中。那么一个简单的方法(假设你的数据表总是按上述方式排序)是:

for i as integer = 0 to datatable.rows.count - 1
    if i < datatable.rows.count - 1 Then
        if datatable.rows(i)(4).value <> datatable.rows(i+1)(4).value then
            newDataTable.rows.Add(datatable.rows(i).itemArray())
        End If
    Else
        newDataTable.rows.Add(datatable.rows(i).itemArray())
    End if
Next

现在语法可能在这里或那里关闭(用 notpad 写),但逻辑的想法就在那里。假设您每次都具有相同的格式,那么我将检查该行何时更改为新的基本 ID 并将该行添加到新的数据表中。从您发布的示例来看,特定基本 ID 的最大版本似乎是该基本 ID 的最后一行。

-克里斯

It depends upon how your populating the original datatable. if you are doing it by querying a database I would format the result set in the query itself something like:

(SQL)

select mid, mname, version, baseID
from <table> t
Join (select baseid, max(version) as version 
         from <table> group by baseID)maxVersion 
    on maxVersion.baseid = t.baseid and maxVersion.version = t.version

this would format the data before its put into the datatable.

If the datatable is populated by other means and you need to filter it then put it in antoher datatable. then an easy way to do it (assuming your datatable is always sorted in the above manner) is:

for i as integer = 0 to datatable.rows.count - 1
    if i < datatable.rows.count - 1 Then
        if datatable.rows(i)(4).value <> datatable.rows(i+1)(4).value then
            newDataTable.rows.Add(datatable.rows(i).itemArray())
        End If
    Else
        newDataTable.rows.Add(datatable.rows(i).itemArray())
    End if
Next

Now the syntax might be off here or there (wrote it in notpad) but the idea of the logic is there. assuming you have the same format each time then I'm checking for when the row changes to a new baseid and adding that row to the new datatable. from the example you posted it looks like the max version of a particular baseid is the last row of that baseid.

-Chris

反目相谮 2024-12-13 17:12:52

我已经使用

Datatables.Compute() 使用 GROUP By 获取最大值

Function GroupBy(ByVal i_sGroupByColumn As String, ByVal i_sAggregateColumn As String, ByVal i_dSourceTable As DataTable) As DataTable

        Dim dv As New DataView(i_dSourceTable)

        'getting distinct values for group column
        Dim dtGroup As DataTable = dv.ToTable(True, New String() {i_sGroupByColumn})
        'adding column for the row count
        dtGroup.Columns.Add("Max_Version", GetType(Double))

        'looping thru distinct values for the group, counting
        For Each dr As DataRow In dtGroup.Rows
            dr("Max_Version") = i_dSourceTable.Compute("MAX(" & i_sAggregateColumn & ")", i_sGroupByColumn & " = '" & dr(i_sGroupByColumn) & "'")

        Next

        'returning grouped/counted result
        Return dtGroup
    End Function

I have used

Datatables.Compute() to get the MAX value using GROUP By

Function GroupBy(ByVal i_sGroupByColumn As String, ByVal i_sAggregateColumn As String, ByVal i_dSourceTable As DataTable) As DataTable

        Dim dv As New DataView(i_dSourceTable)

        'getting distinct values for group column
        Dim dtGroup As DataTable = dv.ToTable(True, New String() {i_sGroupByColumn})
        'adding column for the row count
        dtGroup.Columns.Add("Max_Version", GetType(Double))

        'looping thru distinct values for the group, counting
        For Each dr As DataRow In dtGroup.Rows
            dr("Max_Version") = i_dSourceTable.Compute("MAX(" & i_sAggregateColumn & ")", i_sGroupByColumn & " = '" & dr(i_sGroupByColumn) & "'")

        Next

        'returning grouped/counted result
        Return dtGroup
    End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文