Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8005785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:18:57+00:00 2026-06-04T17:18:57+00:00

I have a List. I would like to filter through all the rows in

  • 0

I have a List. I would like to filter through all the rows in the list of tables to find all the rows that are in every datatable in the list.

If possible, the compare needs to be on the “ID” column that is on every row.

I have tried to solve this with Linq but got stuck. This is what I have so far:

List<DataTable> dataTables = new List<DataTable>();

// fill up the list
List<DataRow> dataRows = 
    dataTables.SelectMany(dt => dt.Rows.Cast<DataRow>().AsEnumerable()).
    Aggregate((r1, r2) => r1.Intersect(r2));

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:18:58+00:00Added an answer on June 4, 2026 at 5:18 pm

    Not a simple question. Here’s a solution (which seems too complicated to me, but it works).

    • Obtain the Id value from each row using Linq to DataSets
    • Intersect the multiple lists to find all the common values
    • Find a single occurence of a row in all of the rows that have one of the matching ids

    To use Linq on DataTable, see this article for a start.

    You could get the ids from one table like this

    var ids = dt.AsEnumerable().Select (d => d.Field<int>("ID")).OfType<int>();
    

    and from multiple tables

    var setsOfIds = dataTables.Select (
        t => t.AsEnumerable().Select (x => x.Field<int>("ID")).OfType<int>());
    

    To intersect multiple lists, try this article. Using one of the methods there you could obtain the intersection of all of the ids.

    Using Jon Skeet’s helper method

    public static class MyExtensions
    {
        public static List<T> IntersectAll<T>(this IEnumerable<IEnumerable<T>> lists)
        {
            HashSet<T> hashSet = new HashSet<T>(lists.First());
            foreach (var list in lists.Skip(1))
            {
                hashSet.IntersectWith(list);
            }
            return hashSet.ToList();
        }
    }
    

    we can write

    var commonIds = setsOfIds.InsersectAll();
    

    Now flatten all the rows from the DataTables and filter by the common ids:

    var rows = dataTables.SelectMany (t => t.AsEnumerable()).Where(
        r => commonIds.Contains(r.Field<int>("ID")));
    

    Now group by id and take the first instance of each row:

    var result = rows.GroupBy (r => r.Field<int>("ID")).Select (r => r.First ());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of Foos that I would like to filter according to
I have a List of objects that I would like to convert to a
Let's say I have a list of domain names that I would like to
I have a list of custom objects List and I would like to update
I have a list of elements which I would like to make sortable and
I have a list of items aprox 60,000 items - i would like to
I have a list of files on a server and would like to load
I have a List of objects which are ordered. I would like to remove
I have a List of object, produced by JPA q.getResultList() . I would like
I have a dropdown list (FK) which I would like to set and display

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.