【问题标题】:query results cannot be enumerated more than once error查询结果不能枚举多次错误
【发布时间】:2010-02-11 16:17:52
【问题描述】:

我有一个问题,我两次调用一个存储的过程,每个都有不同的参数,我需要 2 个单独的列表,但 linq 正在缓存数据并给我上面的错误

我尝试了 2 种不同的方法来绕过错误消息,一种在 tbl 上使用 ToList(),另一种手动遍历数据

我的代码如下所示

获取总数的代码

  public static List<MeterTotalConsumpRecord> GetTotalAllTimesConsumption(DateTime dtStart, DateTime dtEnd, EUtilityGroup ug, int nMeterSelectionType, int nCustomerID, 
        int nUserID, string strSelection, bool bClosedLocations, bool bDisposedLocations)
    {

        dbChildDataContext db = DBManager.ChildDataConext(nCustomerID);

        db.MeterTotalConsumpRecordSet.MergeOption = System.Data.Objects.MergeOption.NoTracking;
        var tbl = from t in db.GetTotalConsumptionByMeter(dtStart, dtEnd, (int) ug, nMeterSelectionType, nCustomerID, nUserID, strSelection, bClosedLocations, bDisposedLocations, 1)                       
                  select t;

        List<MeterTotalConsumpRecord> objList = new List<MeterTotalConsumpRecord>();

        foreach (MeterTotalConsumpRecord objRecord in tbl)
        {
            objList.Add(objRecord);
        }
        return objList;

    }

    public static List<MeterTotalConsumpRecord> GetTotalDayConsumption(DateTime dtStart, DateTime dtEnd, EUtilityGroup ug, int nMeterSelectionType, int nCustomerID,
                int nUserID, string strSelection, bool bClosedLocations, bool bDisposedLocations)
    {
        dbChildDataContext db = DBManager.ChildDataConext(nCustomerID);
        db.MeterTotalConsumpRecordSet.MergeOption = System.Data.Objects.MergeOption.NoTracking;
        var tbl = from t in db.GetTotalConsumptionByMeter(dtStart, dtEnd, (int)ug, nMeterSelectionType, nCustomerID, nUserID, strSelection, bClosedLocations, bDisposedLocations, 3)
                  select t;

        return tbl.ToList();
    }

{

...Code for setting properties using parameters..

_P2Totals = ProfileDataService.DataService.GetTotalAllTimesConsumption(_P2StartDate, _P2EndDate, EUtilityGroup.Electricity, 1, nCustomerID, nUserID, strLocations, bIncludeClosedLocations, bIncludeDisposedLocations);

    _P1Totals = ProfileDataService.DataService.GetTotalAllTimesConsumption(_StartDate, _EndDate, EUtilityGroup.Electricity, 1, nCustomerID, nUserID, strLocations, 
        bIncludeClosedLocations, bIncludeDisposedLocations);


    PopulateLines() //This fills up a list of objects with information for my report ready for the totals to be added

    PopulateTotals(_P1Totals, 1);
    PopulateTotals(_P2Totals, 2);

}

第二次进入 GetTotalAllTimesConsumption 时出现错误

有没有办法可以回到列表的开头?有一个名为 FirstOrDefault 的方法不确定这是否有帮助?

干杯

保罗

【问题讨论】:

  • 请格式化您的代码。

标签: linq linq-to-sql linq-to-objects


【解决方案1】:

您遇到了什么错误?你没有指定它。尝试在每次调用时重新创建数据上下文?

Ps:要在 linq 查询上转换为列表,您只需在 linq 查询的结果上调用 .ToList()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多