查询结果不能枚举不止一次 [英] The query results cannot be enumerated more than once

查看:113
本文介绍了查询结果不能枚举不止一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下的方法。我得到例外,问,而中继器绑定。



Bindrepeater:

 私人无效BindRepeater()
{
VAR IDX = ListingPager.CurrentIndex;
INT ITEMCOUNT;
var关键字= Keywords.Text.Trim();
变种位置= Area.Text.Trim();
无功名单= _listing.GetBusinessListings(位置,关键字,IDX,出ITEMCOUNT);
ListingPager.ItemCount = ITEMCOUNT;
BusinessListingsRepeater.DataSource = list.ToList(); //这里的例外
BusinessListingsRepeater.DataBind();
}



GetBusinessListings:

 公开的IEnumerable<清单> GetBusinessListings(字符串位置,字符串关键字,INT指数,出INT ITEMCOUNT)
{
VAR跳过= GetItemsToSkip(指数);
VAR的结果= CompiledQueries.GetActiveListings(上下文); (!string.IsNullOrEmpty(位置))
如果
{
结果= result.Where(C => c.Address.Contains(位置));
}
如果
{
结果= result.Where(C =>(string.IsNullOrEmpty(关键字)!); c.RelatedKeywords.Contains(关键字)||℃。 Description.Contains(关键字));
}
无功名单=结果;

= ITEMCOUNT list.Count();
返回result.Skip(跳过)。取(10);

}



GetActiveListings:

  ///<总结> 
///返回用户的具体上市
///< /总结>
公共静态只读Func键<的DataContext,IQueryable的<清单>> GetActiveListings =
CompiledQuery.Compile((DataContext的DB)
=>从升的db.GetTable<清单>()
,其中l.IsActive
选择L);


解决方案

在您指定 ITEMCOUNT 您正在执行的查询的第一次。你为什么需要这个?
我的建议是不要检索项数有,只是

 返回result.Skip(跳过)。取(10).ToList(); 



基本上你不能兼得,只获取你需要的结果和检索于一体的总数查询。您可以使用,虽然两个单独querys。


Consider the following methods. I am getting exception as asked , while repeater binding.

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

解决方案

When you assign itemcount you are executing the query the first time. Why do you need this? My suggestion would be not to retrieve the item count there and just

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

Basically you can't have both, fetch only the results you need and retrieve the total count in one query. You could use two seperate querys though.

这篇关于查询结果不能枚举不止一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆