Query results cannot be listed more than once.

Consider the following methods. I get an exception on request, while relay repeater.

Bindrepeater:

private void BindRepeater() { var idx = ListingPager.CurrentIndex; int itemCount; var keyword = Keywords.Text.Trim(); var location = Area.Text.Trim(); var list = _listing.GetBusinessListings(location, keyword, idx, out itemCount); ListingPager.ItemCount = itemCount; BusinessListingsRepeater.DataSource = list.ToList(); // exception here BusinessListingsRepeater.DataBind(); } 

GetBusinessListings:

 public IEnumerable<Listing> GetBusinessListings(string location, string keyword, int index, out int itemcount) { var skip = GetItemsToSkip(index); var result = CompiledQueries.GetActiveListings(Context); if (!string.IsNullOrEmpty(location)) { result= result.Where(c => c.Address.Contains(location)); } if (!string.IsNullOrEmpty(keyword)) { result = result.Where(c => c.RelatedKeywords.Contains(keyword) || c.Description.Contains(keyword)); } var list = result; itemcount = list.Count(); return result.Skip(skip).Take(10); } 

GetActiveListings:

 /// <summary> /// Returns user specific listing /// </summary> public static readonly Func<DataContext, IQueryable<Listing>> GetActiveListings = CompiledQuery.Compile((DataContext db) => from l in db.GetTable<Listing>() where l.IsActive select l); 
+1
source share
2 answers

When you assign an itemcount , you execute the request for the first time. Why do you need it? My suggestion would not be to find an item counter and just

 return result.Skip(skip).Take(10).ToList(); 

Basically, you cannot have both, retrieve only those results that you need and get the total in one query. However, you can use two separate queries.

+5
source

You can save your results as a collection, not IQueryable .

 var list = result.ToArray(); itemcount = list.Length; return list.Skip(skip).Take(10); 

The above code might be wrong for swap. You may have to run the query twice.

+2
source

All Articles