简体   繁体   中英

Lazy loading navigation properties

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM