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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:40:26+00:00 2026-05-17T19:40:26+00:00

My Database contains three tables: PermissionCategories, Permissions, and Users. There is a many to

  • 0

My Database contains three tables: PermissionCategories, Permissions, and Users. There is a many to many relationship between Permissions and Users which is resolved by a UserPermissions table.

Using Entity Framework projection I’m trying to get all the PermissionCategories and include (eager load) the permissions filtered by userId.

My query looks like this:

var data = from pc in ctx.PermissionCategories
            select new
            {
                PermissionCategory = pc,
                Permissions = (
                from p in pc.Permissions
                where p.Users.Any(u => u.UserId == userId)
                select p
                )
            };

A sample iteration to output the results:

foreach (var item in data) 
{
    // category name
    System.Console.Writelin(item.PermissionCategory.Name);

    // DOES NOT CONTAIN THE RESULTS I EXPECT
    System.Console.Writelin(item.PermissionCategory.Permissions);

    // spacer
    System.Console.Writelin("");

    // category name
    System.Console.Writelin(item.PermissionCategory.Name);

    // DOES CONTAIN THE RESULTS I EXPECT
    System.Console.Writelin(item.Permissions);
}

It appears as though the list Permissions included inside PermissionCategory, are re-ordered, putting the expected values first. That same list then also includes all the values which should not appear in the list (Permissions not coming from the userID).

It was my understanding that the EF, using projection, should allow all of the entities to be joined in graphs as they are retrieved. In other words, assign Permissions as seen in “item.Permissions” to the associated entity “item.PermissionCategory.Permissions”.

What am I doing wrong?

Examples:

I have three Permissions. Names = “A”, “B”, “C”.

I have two PermissionCategories. Names = “TestCategory 1”, “TestCategory 2”

I have two users, userID’s 1 and 2.

In my model, users have permissions (a mapped Many to Many). I want my query to get all the PermissionCategories, and include all the Permissions filtered by a specific userID.

Example 1: userId 1 has permissions A and B.

The result for userID 1 should be:

   TestCategory 1
        A
        B
    TestCategory 2

The “permissions” list inside TestCategory 2 would be empty.

Example 2: userId 2 has permissions B and C

The result for userID 2 should be:

   TestCategory 1
        B
    TestCategory 2
        C

Currently, my query produces this:

For userID 1:

   TestCategory 1
        A
        B
    TestCategory 2
        C

For userID 2:

   TestCategory 1
        B
        A
    TestCategory 2
        C

Note the re-ordering of permissions between userID 1 and 2. I don’t know why that happens, or why I’m not getting the expected results.

  • 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-17T19:40:26+00:00Added an answer on May 17, 2026 at 7:40 pm

    Your bug is here:-

     System.Console.Writelin(item.PermissionCategory.Permissions);
    

    You are outputting ALL the persmissions for the permission category. It should be:-

     System.Console.Writelin(item.Permissions);
    

    Which uses the projection you created with just the permissions relating to that user.

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

Sidebar

Related Questions

A legacy database contains a join table which links tables table1 and table2, and
I want to create a SQLite database in my app, which contains three tables,
There is a column in database which contains comma separated values like: 0.00,12.45,14.33 and
I a MSAccess database which contains the following table Table A # Column1    Column2    Amount =======    =======    ======
i have a database which contains an arabic tables , i can read it
This is my ERD for my database contains three tables: I want to retrieve
I have three tables called: users , facilities , and staff_facilities . users contains
So... assuming i have a database with three tables: Table clients Table data and
I have a common database joining situation involving three tables. One table, A, is
I have a database with three tables, a household table, an adults table and

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.