Query result cannot be listed more than once

I use an entity structure (ef) and get the following error:

"The result of the query cannot be listed several times.".

I have a repository class that contains an ef data context. Then I have a controller class (not to be confused with MVC controllers) that contains the storage instance. So far so good ... I have a search method on the controller that should return a RadComboBoxItemData array that is used to populate the Telerik RadComboBox control.

 public RadComboBoxItemData[] Search(int id, string searchText) { var query = context.Search(id, searchText); List<RadComboBoxItemData> result = new List<RadComboBoxItemData>(); foreach (var item in query) { RadComboBoxItemData itemData = new RadComboBoxItemData(); itemData.Text = ""; // assign some text here..; itemData.Value = ""; /*assign some value here..*/ result.Add(itemData); } return result.ToArray(); } 

When I debug my code, I can get into the foreach loop, but then I get the error message:

An exception of type 'System.InvalidOperationException' occurred in System.Data.Entity.dll, but was not processed in the user code

Additional information: The result of a query cannot be listed more than once.

My object uses the import function of an existing stored procedure.

 // EF repository method calling the function imported method on the data context. public IEnumerable<SearchItem> Search(int id, string searchText) { return this.entityContext.Search(id, searchText); } 

The import Search function calls the stored preface to return the SearchItem collection.

I have the feeling that the foreach loop cannot iterate because of something with ef.

+55
c # linq entity-framework entity-framework-4
Apr 19 2018-11-21T00:
source share
3 answers

Try explicitly listing the results by calling ToList() .

Edit

 foreach (var item in query) 

to

 foreach (var item in query.ToList()) 
+112
Apr 19 2018-11-21T00:
source share

Try replacing this.

 var query = context.Search(id, searchText); 

from

 var query = context.Search(id, searchText).tolist(); 

and everything will work well.

+3
Oct 24 '17 at 7:14
source share

if you get this type of error, so I suggest you use the stored data proc as a regular list, and then bind other controls, because I also get this error, so I solved it like this: -

 repeater.DataSource = data.SPBinsReport().Tolist(); repeater.DataBind(); 

try it

-5
Oct. 15 '15 at 12:00
source share



All Articles