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

Posted on 2014-08-05
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?
Question by:hotshottools
    LVL 4

    Expert Comment

    by:Chris Watson
    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

    LVL 62

    Accepted Solution

    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


    Author Closing Comment

    Works like a dream
    LVL 62

    Expert Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now