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

  • Home
  • SEARCH
  • 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 4568504
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:05:09+00:00 2026-05-21T19:05:09+00:00

Starting with a basic class: public class Car { public string Name {get;set;} }

  • 0

Starting with a basic class:

public class Car
{
  public string Name {get;set;}
}

I can then create a list of these cars

List<Car> cars = new List<Car>();

The new step is to have a List of this List, like this:

List<List<Car>> allListsOfCars = new List<List<Car>>();

After populating allListsOfCars, I want to pass it to a function which will return me the cars which exist in every List list.

I know it sounds confusing, so I’ll try explain a bit more.

If I have ListA, ListB, ListC all of type List – and now combine these into 1 holding list(The list of a list), then how can I get back all the cars that exist in every list? For example if a car only exists in ListA, then I’m not interested, it needs to exist in ListA AND ListB AND ListC, then I want it added to the result set and returned.

Thanks in advance.

  • 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-05-21T19:05:10+00:00Added an answer on May 21, 2026 at 7:05 pm

    You need to find the composite intersection of all the sublists.

    IEnumerable<Car> result=allListsOfCars.FirstOrDefault();
    if(result!=null)
    {
        foreach(var sublist in allListsOfCars.Skip(1))
        {
            result=result.Intersect(sublist);
        }
        //enumerate result to run the query
    }
    

    It might be possible to rewrite using the Aggregate operator to eliminate the loop, but Aggregate never reads very well IMO.

    If the lists are long, you’ll probably get a decent speed increase using a HashSet

    IEnumerable<Car> fst=allListsOfCars.FirstOrDefault();
    if(result!=null)
    {
        HashSet<Car> hs=new HashSet<Car>(fst);
        foreach(var sublist in allListsOfCars.Skip(1))
        {
            hs.IntersectWith(sublist); //in-place operation
        }
        //enumerate hs
    }
    

    Make sure that you Car class implements equality members and GetHashCode correctly, otherwise neither of these approaches will work as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Starting with the error: Error 81 The OutputPath property is not set for this
I have a basic question about starting a frame-by-frame animation. When I call the
I'm starting to investigate T4 for Code Generation. I get that you have a
I am new to Rails and am just implementing some basic applications. Just starting
For those just starting to learn WCF , what are the major/basic things I
Starting with 2005, VS started this behavior of when starting debugging session it spawns
Starting from ASP.NET MVC Preview 3, HTML.Button ( and other related HTML controls) are
Starting a new project and would like to use one of the MVC framworks.
Starting with the following LINQ query: from a in things where a.Id == b.Id
Starting recently, some of my new web pages (XHTML 1.1) are setup to do

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.