LINQ to DataTable,查找重复的行 [英] LINQ to DataTable, finding duplicate rows
本文介绍了LINQ to DataTable,查找重复的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用以下DataTable:
Using the following DataTable:
Dim d As New DataTable()
d.Columns.Add("Product", GetType(System.String))
d.Columns.Add("Value", GetType(System.Double))
d.Rows.Add(New Object() {"OAP1", 100.0})
d.Rows.Add(New Object() {"EPP4", 100})
d.Rows.Add(New Object() {"OAP1", 150.25})
d.Rows.Add(New Object() {"OAPIN", 200.0})
我试图使用LINQ来确定是否有不止一种任何类型的产品。在SQL中,这可以像下面这样工作:
I'm trying to use LINQ to identify if there are more than one of any type of product. In SQL, this would work something like:
SELECT Product FROM SOME_TABLE HAVING COUNT(*) > 1
我不能为我的生活在LINQ中如何做到这一点。我正在跟随一个这样做的人:
I can't for the life of me work out how to do this in LINQ. I was following someone who did something like this:
Dim q = From row In d.AsEnumerable()
Group row By New { column1 = row("Product"), column2 = row("Value") }
Into grp
Where grp.Count() > 1
Select row
For Each item In q
' 'q' is not declared. It may be inaccessible due to its protection level.
Next
但是当我尝试使用q时,我收到错误。任何关于我如何使这项工作的想法?
But I get an error when I try to actually use 'q'. Any ideas on how I can make this work?
推荐答案
尝试这个
Dim duplicates = d.AsEnumerable().GroupBy(Function(i) i.Field(Of String)("Product")).Where(Function(g) g.Count() > 1).Select(Function(g) g.Key)
For Each dup In duplicates
Next
这篇关于LINQ to DataTable,查找重复的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文