query results cannot be enumerated more than once error

I have a problem where I call a stored proedure twice, each with different parameters and I need 2 seperate lists but linq is caching the data and also giving me the error above

I have tried 2 different methods to get round the error message, one using ToList() on tbl and the other manually walking through the data

My code is shown below

CODE FOR GETTING THE TOTALS

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); }

I get the error the second time I go into GetTotalAllTimesConsumption

Is there a way I can go back to the start of the list? There is a method called FirstOrDefault not sure if this would help?

Cheers

Paul

-------------Problems Reply------------

What error are you getting? You havent specified it. Try re-creating the data context on each call?

Ps: To convert to a list on a linq query, you can just call .ToList() on the result from the linq query

Category:linq Views:0 Time:2010-02-10

Related post

  • The query results cannot be enumerated more than once? 2009-09-01

    I'm using LINQ to SQL to get a search result of a FullTextSearch stored procedure in Sql server 2008. I dragged the procedure from the server explorer to the designer, and got the method created with the appropriate return type and parameters. Now th

  • How can I solve "The query results cannot be enumerated more than once"? 2010-06-29

    if i try to write below codes : error return to me: The query results cannot be enumerated more than once public void StartTransaction() { using (var stockMovementCtx = new StockMovementCtxDataContext()) using (var scope = new TransactionScope()) { v

  • L2S, Caching, and error: The query results cannot be enumerated more than once 2010-07-11

    I have a fairly complex query (that includes a table valued function to allow full text searching) that I am trying to cache (HttpRuntime.Cache) for paging purposes. When I try to use the cached L2S query, I get the error stated above: The query resu

  • error: The query results cannot be enumerated more than once 2011-07-06

    Edit: DataClassesDataContext dc = new DataClassesDataContext(); string _idCompany = Request["idCompany"]; var newes = dc.GetNewsCompany(Int64.Parse(_idCompany)); string date = ""; string newsHtml = "<center>"; if(newes.GetEnumerator().MoveNext(

  • LINQ error : The query results cannot be enumerated more than once 2011-11-18

    I am working on a LINQ function in which I am using ToList() inside a for loop. At the 1st iteration it works fine but then onward it throws an exception as "The query results cannot be enumerated more than once." The sample code is; for() { function

  • The query results enumerated more than once 2011-03-17

    datacontextclass dc=new datacontextclass (); var news= dc.GetNewsCompany(Int64.Parse ( _idCompany)); if (news.GetEnumerator().MoveNext()) { foreach (var item in news) { drpListNews.Items.Add(item.Title); } } return error:{"The query results cannot be

  • Best practise in splitting up query result by a column 2009-08-03

    I have a query which returns data for a number of dataseries in one lump. I would like to split this data up into the data series id, by the dataseries id. The query cannot be changed. Its not safe to assume that the data is ordered by the series id,

  • C# Active Directory query results limit 2009-10-02

    Is the directorysearcher findall results method capped at 5000 results even if pagesize is set to greater. It really seems to be, because no matter what I get exactly 5000 results. This is C# --------------Solutions------------- First of all, it's a

  • How do I disable query results when executing a stored procedure from a stored procedure? 2008-10-17

    Within a stored procedure, another stored procedure is being called within a cursor. For every call, the SQL Management Studio results window is showing a result. The cursor loops over 100 times and at that point the results window gives up with an e

  • Exporting crosstab query results to Excel from within MS Access 2008-12-08

    I've been trying with limited success to export a crosstab query result set to Excel using Access 2003. Occasionally, the export works correctly, and Excel shows with no errors. Other times, using the exact same query parameters, I get a 3190 error -

  • Output SQL query results to flat-file with Java 2008-12-12

    Is there an easy or straightforward way in Java to output the results of a DB Query to a file (either csv, tab, etc). Perhaps even in Hibernate? I know that a query results can be dumped to a flat file on the DB Server. I am looking for a way that an

  • How do I sort query results by keyword occurrence in column? (SQL Server Full-Text) 2009-01-07

    How do I sort the query results by keyword occurrence in column? I'm using SQL Server 2008 with Full-text indexing. --------------Solutions------------- select columnname, count(columnname) as cnt from tablename group by columnname order by count(col

  • How to get only second,third,fourth or fifth row of a query result?(SQL SERVER 2000) 2009-02-11

    I would need a suggestion or even a solution, how to get only the second,third... row of a query result with high performance. I know there is the possibility of row_number(SQL Server 2005 or higher) but not for SQL Server 2000 :-( My first try to ge

  • Format SQL query results 2009-02-17

    I am looking for a way to set the width on the returned column result from a SQL query. I run a query like Select Product from ConfigScheduling where Product is an Nvarchar(Max) column. The result in SQL Server Management Studio with Results to Grid

  • Caching query results in ORMs - satisfying arbitrary subset queries 2009-03-12

    I know N/Hibernate uses a cache to satisfy queries that it has seen before, and that is called a query cache. However, can it satisfy a subset of that query? I'd imagine not since I'd guess that the general problem of figuring that out is undecidable

  • Lazy loading/caching of SQL query results with a model 2009-03-29

    I'm developing a system (with Rails 2.3.2, Ruby 1.8.7-p72) that has a sizable reporting component. In order to improve performance, I've created a Report model to archive old reports. The idea is that if a matching report already exists for an arbitr

  • How do you output MySQL query results in csv format (to the screen, not to a file)? 2009-04-01

    I'm trying to output query results in comma delimited format using the mysql command line tool. My mysql user does not have access to use the "INTO OUTFILE" option referenced in this question: http://stackoverflow.com/questions/356578/how-to-output-m

  • how can i add select query result into dataset later again select query run 2009-04-08

    i want to add select query result into dataset, so i can write new query to run on it to get net dataset but how? Original query: MyDATASET=( select x, y,z from table1 union all select k,l,m from table2 ) i wan to this select * from this.MyDATASET --

  • Add row to query result using select 2009-04-10

    Hey guys, is it possible to extend query results with literals like this? Select name from users union select name from ('JASON'); or Select age, name from users union select age, name from (25,'Betty'); so it returns all the names in the table plus

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.161 (s). 11 q(s)