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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:44:32+00:00 2026-06-18T04:44:32+00:00

I got the following structure: One user can have many Groups and a Group

  • 0

I got the following structure:

 One user can have many Groups and a Group can have many users.

Now I want to get all users that are in the same group like the user to filter by.

For example,
The user “Theo” is in Group one and two. I want all users that are in Group one OR Group two.

How can I achive this with LINQ or generally c#?

This code doesn’t work:

var res = (IEnumerable<User>)Users;
foreach (var item in user.Groups) {
    res = res.Where(usr => usr.Groups.Contains(item));
}
return res.ToList();

Users is a list I got from another method and user is a parameter of type User.

  • 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-18T04:44:33+00:00Added an answer on June 18, 2026 at 4:44 am

    I assume:

    • The User class has a property Groups of type List<Group>
    • The Group class has a property Users of type List<User>

    Use the LINQ SelectMany method:

    var usersInSameGroups = user.Groups.SelectMany(group => group.Users).ToList();
    

    Or in query-syntax:

    var usersInSameGroups = (from g in user.Groups
                             from userInGroup in g.Users
                             select userInGroup).ToList();
    

    UPDATE

    return (from user2 in Users
            where user2.Groups.Intersect(user.Groups).Any()  // Keeps only user2 if it has a common group with user
            select user2).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following database structure: Table 1 Table 2 Table 3 tid_1 ----(many-to-one)----
I have got following xml structure of my app activity. Now I would like
I have got the following class structure, and have plenty of classes like C
I got following repeating syntax and I want to keep all <h1> that is
I've got this really basic table structure: dbo.tblCategory dbo.tblQuestion (many to one relationship to
I have the following table structure. One table EQUITIES and a table for each
I have a structure like the following and am trying to remove one item
I have got following folder structure for my domain (domain.com): /data/images (it´s subdomain name,
I've got a table X with the following structure in sql server 2008R2 db
I got a simple tree structure which contains the following public class MyTree {

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.