【问题标题】:Getting duplicates using Linq on strongly typed table在强类型表上使用 Linq 获取重复项
【发布时间】:2017-07-25 17:30:30
【问题描述】:

我正在根据名称、日期和类别都相同的表中查找重复项。我想要我的结果集中的两个(或所有)重复行。我将 VS 2008 与 .net 3.5 一起使用。我从强类型表中的数据开始,并希望最终只在同一个表或表的副本中重复。我使用数据表作为报告的来源。

我花了很多时间检查类似的问题,但没有找到答案。

Dim LList as PmtDataTable = PmtList.GroupBy(x => New {x.Name, x.PmtDate, x.Category}) _
                   .Where(x => x.Count() > 1) _
                   .select(x => New {x.key, Values = x.tolist()})

我从https://stackoverflow.com/a/15747265/2559297 获得了语法,但由于我使用的是旧 VS,所以它不适合我。你能用 .net 3.5 把它翻译成 VS 2008 吗?

【问题讨论】:

    标签: vb.net linq


    【解决方案1】:

    您错过了关键字Key

    而你错过了C#VB.NET :)(我是 C# 开发人员)。

    试试这个:

    Dim query = PmtList.GroupBy(Function(x) New With {Key .Name = x.Name, Key .PmtDate = x.PmtDate, Key .Category = x.Category}) _ .Where(Function(x) x.Count() > 1) _ .Select(Function(x) New With {.KeyName = x.key, .Count= x.Count()})

    【讨论】:

    • Dim LList as PmtDataTable 更改为 Dim query 应该可以工作
    • 不知何故我需要将 linq 结果返回到数据表
    • 感谢语法方面的帮助。为此,我接受这个作为答案。请参阅我的答案以了解如何将其恢复到数据表中。
    【解决方案2】:

    @Tim.Tang 的回答很接近。但我需要它来反馈到 PmtDataTable。因此更改了他的 Select 语句以返回组。然后我得到这些行并将数据添加到一个新的数据表中。

    Dim dupList = PmtList.GroupBy(Function(x) New With {Key .Name = x.Name, _
                                                        Key .PmtDate = x.PmtDate, _ 
                                                        Key .Category = x.Category}) _
                         .Where(Function(y) y.Count() > 1) _
                         .SelectMany(Function(grp) grp)
    
    Dim PmtList2 As New PmtDataTable
    For Each rw as PmtRow In dupList
        Dim iRow As PmtRow
        iRow = PmtList2.NewPmtRow
        iRow.ItemArray = rw.ItemArray
        PmtList2.AddPmtRow(iRow)
    Next
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-20
      • 2011-04-04
      • 1970-01-01
      • 2013-01-05
      • 2012-02-05
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      相关资源
      最近更新 更多