Linq query to return a subset of a dataset providing 1 or more of a specific field match

I have a datatable as below:
ID          GroupKey        Name
11          aa                     Bob
13          bb                     Dave
23          aa                     Karen
34          cc                      Jay
54          bb                     Jane

What Id like to have is a Linq query that will return the records where the count of the groupKey is > 1.  So in this case it will return:

ID          GroupKey        Name
11          aa                     Bob
23          aa                     Karen
13          bb                     Dave
54          bb                     Jane

Can this be done simply in Linq?
hotshottoolsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris WatsonSoftware DeveloperCommented:
Assuming items contains your data as listed in your first table in your question, the LINQ query below will return the required subset.

Dim query = From item In items
                    Where items.LongCount(Function(meti) meti.GroupKey = item.GroupKey) > 1

Open in new window

0
Fernando SotoRetiredCommented:
Hi hotshottools;

The below snippet will query the DataSet object, and I am assuming table zero but change as needed. and returns the required values.

Dim results = From row In DataSetObj.Table(0).AsEnumerable() _
              Group row By Key = row.Field(Of String)("GroupKey") Into dtGroup = Group _
              Where dtGroup.Count > 1 _
              Order By Key _
              From dtg In dtGroup _
              Select dtg

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hotshottoolsAuthor Commented:
Works like a dream
0
Fernando SotoRetiredCommented:
Glad it worked out for you hotshottools.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.