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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:58:18+00:00 2026-05-13T15:58:18+00:00

Im using the Repository pattern and I want to write a method that receives

  • 0

Im using the Repository pattern and I want to write a method that receives a role and returns an Iqueryable of the users that belong to that role. (Im not sure if the right way would be to receive the role object or the role_id… in any case, how can I do this?? I dont like the query structure, I prefer the method structure of linq.
users and roles is many to many with a users_roles join table.

     private ClasesDataContext db = new ClasesDataContext();
        public IQueryable GetByRole(Role role)
        {
            return db.Users.Where();
        }
  • 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-13T15:58:18+00:00Added an answer on May 13, 2026 at 3:58 pm

    Maybe try something like:

    public IQueryable<User> GetByRoleId(Role role) {
       return db.UsersRoleJoinTable.Where(ur => ur.Role == role).select(ur => ur.User);
    }
    

    Where UsersRoleJoinTable is your many-to-many join table.

    Hope it helps.

    Update: the select(ur => ur.User) is telling linq that for every row returned by “db.UsersRoleJoinTable.Where(ur => ur.Role == role)” we want to get the user associated with the UsersRoleJoinTable object. If you wanted a list of user ids instead, you could tell linq to select only user.id by doing select(ur => ur.id). Think of linq’s select as a some sort of “for every row do this and put it in the list returned instead of the original row”

    There is one downside to this approach tho, I believe in this case Linq is generating the sql to get the rows from the Join table (UsersRoleJoinTable) and then for every row returned, is executing another query to look up the User. I might be wrong on this, so to check the SQL generated by Linq do:

    string sql_query = db.UsersRoleJoinTable.Where(ur => ur.Role == role).select(ur => u.User).ToString();
    

    and then print the value of sql_query or watch it in debug mode. If Linq is in fact doing multiple queries, then I think the best solution is to create a view or stored procedure in SQL Server to get the users associated with the role and then add the view or stored procedure to Visual Studio designer so that you can call the view like:

    db.GetUsers(role_id) //if using a GetUsers stored procedure
    

    or

    db.UsersByRoleView.where(ur => ur.role_id == passed_role_id) //if using a UsersByRoleView view
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 356k
  • Answers 356k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Instead of JasperPrint print = JasperFillManager.fillReport(report, parameters); I used JasperPrint… May 14, 2026 at 8:50 am
  • Editorial Team
    Editorial Team added an answer Yes, an enum is exactly what you need. General practice… May 14, 2026 at 8:50 am
  • Editorial Team
    Editorial Team added an answer Personally, I would recommend ReSharper as it provides functions for… May 14, 2026 at 8:50 am

Related Questions

I'm trying to get to grips with NHibernate, Fluent NHibernate and Spring. Following domain-driven
I am finishing up a rewrite of a project management tool using ASP.NET MVC,
I'm using DDD and NHibernate to persist my domain object. In my database every
So I'm implementing the repository pattern in an application and came across two issues
I'm really struggling to wrap my head around some of this stuff. Let me

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.