The Result of a Query Cannot Be Enumerated More Than Once

The result of a query cannot be enumerated more than once

Try explicitly enumerating the results by calling ToList().

Change

foreach (var item in query)

to

foreach (var item in query.ToList())

The Result of a Query Cannot be Enumerated More than Once (C#) (Entity Framework) (Stored Procedure)

You need ToList on the first db call because you are enumerating the LabelIDs value multiple times. LabelIDs.Count runs the query the first time, then LabelIDs.ElementAt runs it again later on.

The result of a query cannot be enumerated more than once.

In BindListView, when you do .ToList(), it enumerates the query for the first time. And what you store in the session is the query itself. When you do .ToList() again in txtSearch_TextChanged, it enumerates the query a second time, which is not supported.

You should store the result of .ToList() in the session, rather than the query:

Session["LookupValues"] = lvLookup.DataSource = res.ToList();

LINQ : The query results cannot be enumerated more than once

Materialize your query:

var addresses = (from p in db.SearchFirst(city, area)
select new
{
ID = p.Id,
Address = p.address,
Level = p.agahilevel

})
.Skip(Convert.ToInt32(start))
.Take(Convert.ToInt32(width))
.ToList();

Then use Enumerable.Any to check if it contains elements:

if(addresses.Any())
{
int index = 0; // ...
}

The result of a query cannot be enumerated more than once when using stored procedure

Its better to first operate on the Query and then return the enumerable at last.

public IEnumerable<BrandNameToIngredient> GetBrandNameToIngMapResults(string Keyword)
{
var brandNametoIngQuery = DB.USP_BRANDNAME_INGREDIENT_MAP()
.Where(x => string.IsNullOrEmpty(Keyword)
|| x.BrandName.Contains(Keyword, StringComparison.OrdinalIgnoreCase)
|| x.PFCName.Contains(Keyword, StringComparison.OrdinalIgnoreCase)
|| x.IngredientName.Contains(Keyword, StringComparison.OrdinalIgnoreCase)
|| x.HCIngredientName.Contains(Keyword, StringComparison.OrdinalIgnoreCase))
.Select(map=> new BrandNameToIngredient
{
IngredientBrandNameMapID = map.INGREDIENT_PRODUCT_MAP_ID,
BrandName = map.FDA_BRAND_NAME, //From Table 1
PFCName = map.PFC_DESC == null ? "" : map.PFC_DESC, //From Table 1
IngredientName = map.INGREDIENT_NAME, //From Table 2
HCIngredientName = map.HC_INGREDIENT_NAME, //From Table 2
KeywordfromPage = Keyword
}).AsEnumerable();
}

The Query Results cannot be enumerated more than once'

You are trying to enumerate the data more than once - once in the foreach and then again with the .ToList() for each matching UserGuid, which is causing the error.

Perhaps this LINQ select statement will help:

public void GetSearchString()
{
Data.Database.FRCDatabaseDatacontext context = new Data.Database.FRCDatabaseDatacontext();

lstName.ItemsSource = (from s in context.GetSearchProcedure(txtName.Text)
where s.UserGuid == Workspace.Instance.ActiveUser.CurrentUserActiveDirectoryGuid
select s).ToList();
}

The result of a query cannot be enumerated more than once

Your query memIDs.Take(Environment.ProcessorCount) always takes the first Environment.ProcessorCount items from memIDs.

It doesn't "remember" that you took a certain number of items the next time you call .Take on it - it will always start from the beginning again.

Because you're calling it in a loop, memIDs is being repeatedly enumerated. I'm thinking that's where the error is coming from.

You could fix this by turning it into a list like so:

var memIDsList = memIDs.ToList();

And then use memIDsList instead of memIDs.

However, that won't help with your faulty query logic. I assume you're trying to take N items at a time and do something with them?

If so, you could use MoreLinq's Batch extension. (See Create batches in linq for more info about Batch.)

I don't know if using Batch would avoid the need for .ToList(), but you should try it without.

EF 6: The result of a query cannot be enumerated more than once

No, you can't achieve your desired result like that. As an alternative you can, however, save your filters to variables and then compose them as needed:

 Func<Trade, bool> filter1 = t => t.A > 10;
Func<Trade, bool> filter2 = t => t => t.B > 10;
Func<Trade, bool> compositeFilter = t => filter1(t) && filter2(t);

int totalCount = _db.Trades.Count(filter1);
int totalCountAfterAdditionalFilter = _db.Trades.Count(compositeFilter);

//Need more?
compositeFilter = t => compositeFilter(t) && t.C > 100;
int totalAfterMoreFilters = _db.Trades.Count(compositeFilter);


Related Topics



Leave a reply



Submit