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 6182623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:13:50+00:00 2026-05-24T01:13:50+00:00

I have two Generic Lists containing different types, for the sake of example, lets

  • 0

I have two Generic Lists containing different types, for the sake of example, lets call them Products and Employees. I’m trying to find Products that are based at the same location as Employees, i.e. where product.SiteId == emp.SiteId

List<Product> lstProds;
List<Employees> lstEmps;

My (old skool) brain is telling me to use a forEach loop to find the matches but I suspect there is a (‘better’/terser/faster?) way to do it using Linq. Can anyone illuminate me? All the examples I’ve found online deal with Lists of primitives (strings/ints) and are not especially helpful.

  • 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-24T01:13:52+00:00Added an answer on May 24, 2026 at 1:13 am

    I would say:

    var products = from product in lstProds
                   join employee in lstEmps on product.SiteId equals employee.SiteId
                   select product;
    

    However, if there are multiple employees with the same site ID, you’ll get the products multiple times. You could use Distinct to fix this, or build a set of site IDs:

    var siteIds = new HashSet<int>(lstEmps.Select(emp => emp.SiteId));
    
    var products = lstProds.Where(product => siteIds.Contains(product.SiteId));
    

    That’s assuming SiteId is an int – if it’s an anonymous type or something similar, you may want an extra extension method:

    public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source)
    {
        return new HashSet<T>(source);
    }
    

    Then:

    var siteIds = lstEmps.Select(emp => emp.SiteId).ToHashSet();
    var products = lstProds.Where(product => siteIds.Contains(product.SiteId));
    

    Alternatively, if you have few employees, this will work but is relatively slow:

    var products = lstProds.Where(p => lstEmps.Any(emp => p.SiteId == emp.SiteId));
    

    Add a ToList call to any of these approaches to get a List<Product> instead of an IEnumerable<Product>.

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

Sidebar

Related Questions

I've got two collections (generic Lists), let's call them ListA and ListB. In ListA
I have two Generic Lists with the same objects Type T within them. For
I have a two generic abstract types: Entity and Association . Let's say Entity
I have two EmailAddress generic lists, I wanted a simple way of just getting
Let's say I have two generic lists of the same type. How do I
I have two generic lists of type T. Both lists contain same type, and
I have two generic lists. Let's say they are List< A > and List<
I have two generic lists where I want to run a couple of Linq
I am trying to find the difference between two generic lists, as in the
I have a generic list which I form from two other lists. The orderby

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.