考虑以下方法。我被问到异常,而转发器绑定。
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);
答案 0 :(得分:5)
分配itemcount
时,您是第一次执行查询。你为什么需要这个?
我的建议是不要在那里检索项目计数而只是
return result.Skip(skip).Take(10).ToList();
基本上你不能同时拥有两者,只获取你需要的结果并在一个查询中检索总计数。你可以使用两个单独的查询。
答案 1 :(得分:2)
您可能希望将结果保留为集合,而不是IQueryable
。
var list = result.ToArray();
itemcount = list.Length;
return list.Skip(skip).Take(10);
上述代码可能不适合分页。您可能需要运行两次查询。