如何修复查询结果不能在foreach循环中多次枚举错误 [英] How to fix the query results cannot be enumerated more than once error in foreach loop

查看:75
本文介绍了如何修复查询结果不能在foreach循环中多次枚举错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在发送jquery ajax调用以获取此数据,因此在此调用期间,有时我会收到此错误



I am sending jquery ajax call to get this data , so during this call , sometimes i recevive this error

public static List<object> GetExcelDataforOjective(GoogleChartReportFilters objFilter, int? companyId, string ObjectiveStatus)
        {
            ActusSystemDataContext _db = new ActusSystemDataContext();
            int dataForYear = Company.GetCompaniesCurrentBusinessyear().company_StartDate.Year;
            System.Data.Linq.ISingleResult<usp_GetEmployeeListForObjectiveChartResult> objectiveResult = null;
            objectiveResult = _db.usp_GetEmployeeListForObjectiveChart(dataForYear,
                                                                    companyId, objFilter.DateFilter, objFilter.MoreThanLessThanStatus,
                                                                    objFilter.MoreThanLessThanValue, objFilter.ObjectivesStatus, objFilter.ObjectivePeriod);
            List<object> lstObj = new List<object>();

            foreach (var result in objectiveResult.ToList())
            {
                if (result.Objective_Status == ObjectiveStatus)
                {
                    ActusLibrary.Objectives obj = new ActusLibrary.Objectives();

                    obj.ObjectiveStatus = result.Objective_Status;
                    obj.EmpName = result.EmployeeName;
                    obj.Business_unit = result.Business_unit;
                    obj.Department = result.Department;
                    obj.Team = result.Team;
                    obj.Location = result.Location;
                    obj.ManagerName = result.ManagerName;
                    lstObj.Add(obj);
                }
            }
            return lstObj;
        }

推荐答案

我假设你有并发问题因为 objectiveResult 显然是一个类成员而不是局部变量。因此,当对此方法的一次调用即将在 objectiveResult 上执行 ToList()时,另一个调用会在完成时覆盖它查询调用 _db.usp_GetEmployeeListForObjectiveChart(..),然后两者都尝试在同一个上执行 ToList()查询结果对象。使 objectiveResult 该方法的局部变量,它应该是好的。



编辑:变量声明范围的说明:

I assume you have a concurrency problem there because objectiveResult apparently is a class member and not a local variable. So while one call to this method is about to execute ToList() on objectiveResult, another call overwrites it as it finishes the the query call to _db.usp_GetEmployeeListForObjectiveChart(..) and then both try to execute ToList() on the same query result object. Make objectiveResult a local variable of that method and it should be good.

edit: Illustration of variable declaration scope:
class SomeClass
{
    int SomeMemberVariable; // declared as class member

    void SomeMethod()
    {
        int someLocalVariable; // declared locally in method

        SomeMemberVariable = 1; // class member (analog to objectiveResult)
    }
}



您在此示例中将 objectiveResult 声明为类成员,例如 SomeMemberVariable 。您需要在本地声明它,例如 someLocalVariable


You declared objectiveResult as a class member like SomeMemberVariable in this example. You need to declare it locally like someLocalVariable instead.


这篇关于如何修复查询结果不能在foreach循环中多次枚举错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆