使用 linq vb.net 分组和计数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26874187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:04:50  来源:igfitidea点击:

group by and count with linq vb.net

vb.netlinqcountgroup-by

提问by derstauner

I have a datatable. I would like to list the double entries. This would I make with a simple sql query like this:

我有一个数据表。我想列出双重条目。这将使用一个简单的 sql 查询来进行,如下所示:

SELECT artnr, COUNT(artnr) AS cnt FROM table GROUP BY artnr HAVING COUNT(artnr)>1

How should this looke with linq?

这应该如何与 linq 搭配使用?

Every time, when I need to work with linq, I get always stuck with it. I dislike it really.

每次,当我需要使用 linq 时,我总是被它卡住。我真的不喜欢。

Thanks.

谢谢。

回答by crackhaus

Not sure what your table looks like, but give this a try. Also I'm sure there are plenty of answers on stack that illustrate grouping in VB. This might be a duplicate question.

不确定你的桌子是什么样子,但试试这个。此外,我确信堆栈上有很多答案可以说明 VB 中的分组。这可能是一个重复的问题。

Dim artnrGroups = From a In table _
                    Group a By Key = a.artnr Into Group _
                    Where Group.Count() > 1
                    Select artnr = Key, numbersCount = Group.Count()

回答by thewyzard

And you can put the Count() into that query like this

您可以像这样将 Count() 放入该查询中

Dim duplicateRows = From row In table _
           Group row By row.artnr Into g _
           Where g.Count() > 1 _
           Select row