查询结果不能多​​次枚举错误

时间:2010-02-10 12:58:04

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

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

我尝试了两种不同的方法来绕过错误消息,一个使用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的方法,不确定这是否有帮助?

干杯

1 个答案:

答案 0 :(得分:0)

你得到什么错误?你没有指定它。尝试在每次通话时重新创建数据上下文?

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