2017-07-24 31 views
-1

我正在查找基於名稱,日期和類別的表中的副本全部相同。我想要結果集中的兩個(或全部)重複行。我使用VS 2008和.net 3.5。我開始使用強類型表中的數據,並希望最終只在同一個表或副本中存在重複項。我使用數據表作爲報告的來源。在強類型表上使用Linq獲取重複項

我花了很多時間檢查類似的問題,但沒有找到答案。

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嗎?

回答

1

你錯過了關鍵字重點

你錯過了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()})

+0

變化'昏暗LLIST爲PmtDataTable'到'昏暗query'應該工作 –

+0

不知怎的,我需要得到LINQ結果返回到DataTable –

+0

感謝您與語法的幫助。爲此,我接受這個答案。請參閱我的答案以瞭解如何將其返回到數據表。 –

0

@ 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