Error: A query's result can't be enumerated more than once.

domainservices entity-framework

Question

Call SP in EF 4.0 using the code below:

  public IQueryable<Employee> GetEmployeesByFollowup()
        {
            var results = from p in this.ObjectContext.SearchEmployeeFollowup()
                          select p;
            foreach (Employee p in results)
            {
                p.DepaermentReference.Load();                
            }
            return results.AsQueryable<Employee>();
        }

The for loop produced the following error:

It is not possible to enumerate a query's results more than once. System.InvalidOperationException (System.SystemException)

It appears to be functioning well for a while. Understand why not.

1
1
7/20/2010 8:15:31 PM

Popular Answer

delivering aIQueryable from a method is only helpful if you wish to modify the query before it is performed (filter, projection, join, etc.). However, because your approach lists the outcomes withforeach It is too late to change anything to the query by the time you return it since it has already been run.

Maybe your algorithm should return aIEnumerable<Employee> instead:

    public IEnumerable<Employee> GetEmployeesByFollowup()
    {
        var results = this.ObjectContext.SearchEmployeeFollowup().ToList();
        foreach (Employee p in results)
        {
            p.DepaermentReference.Load();
        }
        return results;
    }

By the way, because EF 4.0 manages slow loading of linked entities, you often don't need to callLoad explicitly

4
7/20/2010 9:54:29 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow