?
Solved

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

Posted on 2014-08-05
4
Medium Priority
?
137 Views
Last Modified: 2016-02-16
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?
0
Comment
Question by:hotshottools
  • 2
4 Comments
 
LVL 5

Expert Comment

by:Chris Watson
ID: 40241892
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
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 40241996
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
 

Author Closing Comment

by:hotshottools
ID: 40242070
Works like a dream
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40242073
Glad it worked out for you hotshottools.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question