【问题标题】:Selecting duplicate values in DataTable with multiple conditions在具有多个条件的 DataTable 中选择重复值
【发布时间】:2018-07-04 16:07:34
【问题描述】:

我有一个数据表 (DT),我必须在指定列中选择重复值,并且在其他列上具有多个条件,并获取该行的 Id。 不幸的是,我是这方面的初学者。

我的数据表包含以下列:

Id, PartNo, Group 

我在下面找到了这段代码,如果我想在一列中获取多个值,但我无法展开它。

Dim duplicates = From row In DT.AsEnumerable() _
    .GroupBy(Function(i) i.Field(Of String)("PartNo")) _
    .Where(Function(g) g.Count() > 1) _
    .Select(Function(g) g.Key)

For Each dup In duplicates
    Debug.Print(dup)
Next

所以我想

- get the "Id" values on those rows
- where ["Group"] LIKE "Assembly" OR ["Group"] LIKE "Part" 
 AND
- "PartNo" value can found more than once

示例: 在此表中,我想获得“Id”值:1、3、5、6、8

+----+--------+----------+
| Id | PartNo |  Group   |
+----+--------+----------+
|  1 |   1111 | Assembly |
|  2 |   1111 | Common   |
|  3 |   2222 | Part     |
|  4 |   2222 | Common   |
|  5 |   2222 | Part     |
|  6 |   1111 | Part     |
|  7 |   3333 | Assembly |
|  8 |   2222 | Assembly |
+----+--------+----------+

谢谢

【问题讨论】:

    标签: mysql vb.net linq datatable


    【解决方案1】:

    您可以在进行分组之前添加另一个 .Where 子句以选择多个零件编号,然后 .SelectMany 将组展平为整数列表:

    Dim duplicates = From row In DT.AsEnumerable() _
    .Where(Function(i) i.Field(Of String)("Group") = "Part" OrElse i.Field(Of String)("Group") = "Assembly") _
    .GroupBy(Function(i) i.Field(Of String)("PartNo")) _
    .Where(Function(g) g.Count() > 1) _
    .SelectMany(Function(g) g.Select(Function(c) c.Field(Of Integer)("Id")))
    

    【讨论】:

    • 感谢您的帮助。在最后一行中,“Select(c >= c”出现了一些错误。VB 用红色下划线标记了这一点
    • 效果很好! :) 你明白了 :) 顺便说一句,我错过了一些东西 :D 如果还有另一列“路径”。变化在于,如果“PartNo”值可以多次找到“路径”不同的地方:D 你能帮我解决这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    相关资源
    最近更新 更多