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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:27:55+00:00 2026-05-27T06:27:55+00:00

I have a problem with this piece of code, when I do my intersect

  • 0

I have a problem with this piece of code, when I do my intersect method everything works fine.

When I do the count on my intersection before the foreach I have 1.

After the foreach, if do the count again after the foreach I have 0, why is this happening? It should always be 1…

var matchedRoles = roles.Intersect(user.Roles);
int before = matchedRoles.Count();

foreach (var matchedRole in matchedRoles)
{
    user.Roles.Remove(matchedRole);
}

int after = matchedRoles.Count();
if (matchedRoles.Any())
{
    accountRepository.Update(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-05-27T06:27:56+00:00Added an answer on May 27, 2026 at 6:27 am

    It happens because LINQ queries are lazy-evaluated: the result is not produced until it needs to be (which is whenever you call Count). It stands to reason that if you modify user.Roles in the meantime, the Count computed after the modification will be different.

    If you want to “fix” the result, then you have to force LINQ to make a local copy of the results, e.g. like this:

    // Here, ToArray() forces LINQ to immediately produce the results
    var matchedRoles = roles.Intersect(user.Roles).ToArray();
    

    This way, any further operations on matchedRoles will work on a fixed “snapshot” and produce the same results as long as you do not modify matchedRoles itself.

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

Sidebar

Related Questions

I've wrote this simple piece of code. And I have a slight problem with
I have problem with this piece of code: var el = $('div#editor'); el.find('*[contentEditable]').removeAttr('contentEditable'); It
I have a problem with this piece of code when I build it for
I have a problem with this piece of code If I don't use the
I have a problem with this piece of code in C. #include <stdio.h> #include
Currently learning Scheme/Racket and have problem running this piece of code. (if (or (<
hello everyone I have some problem with understanding this piece of the code: import
I have a problem with content_tag into an helper, this piece of code def
I have a problem with this piece of code : var logo = document.getElementById(move_this);
I have problem running and debugging this piece of code: bool readSectionHeaders(char* path, int

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.