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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:19:25+00:00 2026-06-02T00:19:25+00:00

It seems I am unable to put a where clause on the include in

  • 0

It seems I am unable to put a where clause on the include in my domain service – so we are doing some loops to get the data we need for some lookupGroups.

   var _lookupGroups = _lookupGroupRepository.All();
        var _lookupValues = _institutionLookupValueRepository.All().Where(x => x.InstitutionID == _userProfile.InstitutionID);

        int i = 0;

        foreach (var _group in _lookupGroups)
        {
            var _values = _lookupValues.Where(x => x.LookupGroupID == _group.LookupGroupID);

            foreach (var _value in _values)
            {
                _group.InstitutionLookupValues.Add(_value);

                i++;
            }

            Console.WriteLine(_group.GroupName + " " + i.ToString());
        }

        return _lookupGroups;

The counter I put in was for some validation on what i was seeing – the first group that we iterate through is States. my counter will say 50 but my navigation property will say 100, showing one set of 50 for each Institution we have (we have 2 sets). so it is going and getting all the lookupvalues and putting them in the navigation property even though my counter says 50. also i have removed all includes in the metadata file.

To clarify: i have lookupvalues that are attached to lookupGroups. the lookup values are assigned to institutions. so if i go get the values for the lookup group of STATES i would get back 50 lookup values. the code above. when i run this line _group.InstitutionLookupValues.Add(_value); my InstitutionLookupValues = 100 (taking all lookupValues regardless of institution). but my counter shows 50. somewhere I am getting all the lookup values not sure the ones tied to a specific institution.

  • 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-02T00:19:27+00:00Added an answer on June 2, 2026 at 12:19 am

    I think this is caused by the fact you’re accessing a modified closure, and, even if it isn’t, you shouldn’t be anyway. The line

    var _values = _lookupValues.Where(x => x.LookupGroupID == _group.LookupGroupID);
    

    probably isn’t doing what you think it’s doing (unless it’s C#5). The lambda is a delegate and the closure will close over the variable, not the value. This means your code _group.LookupGroupID will always be the last value from the final iteration of the enumerator, so you’ll only ever filter the lookup values on one _group.LookupGroupID.

    Try changing the block to:

    foreach (var _group in _lookupGroups)
    {
      var currentGroup = _group;    
      var _values = _lookupValues.Where(x => x.LookupGroupID == currentGroup.LookupGroupID);
    
      foreach (var _value in _values)
      {
         currentGroup.InstitutionLookupValues.Add(_value);
    
         i++;
      }
    
      Console.WriteLine(currentGroup.GroupName + " " + i.ToString());
    }
    

    I’m not sure whether that’s the cause of the problem, but you’ll get other weird effects anyway. By creating your own variable within the loop, you avoid closing over the generated enumerator variable.

    Take a look at Eric Lippert’s articles on this: Closing over the loop variable considered harmful. It’s surprising how few people are aware of this issue.

    Also, it’s probably a good idea to collapse the query:

    var _lookupValues = _institutionLookupValueRepository.All().Where(x => x.InstitutionID == _userProfile.InstitutionID);
    

    by using ToList(), or similar. Otherwise you’ll be re-evaluating this query for each loop in the _lookupGroups and this could be a nasty performance hit if either the query is to a database, or the loop is large.

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

Sidebar

Related Questions

Using MongoDB C# driver ( http://github.com/samus/mongodb-csharp ), seems that I'm unable to get the
This seems like it should be really simple, but I'm unable to figure this
This seems like it would be fairly simple, but I've been unable to find
I've been unable to find any info on this, which seems like it could
For my html form, it seems that I'm unable to create cross browser hover
Myself and some teammates have been unable to understand why the following snippet of
I am unable to get the frame to refresh first, THEN jump to the
(Note: This is a follow up to my question Can jQuery.getJSON put a domain's
My Problem is quickly described by the need to extend the session data life
I'm trying to passing an arguments (array) for update, but seems CouchDB unable to

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.