[英]The result of a query cannot be enumerated more than once.
I have a listview dialog to bind a data to that listview. 我有一个列表视图对话框将数据绑定到该列表视图。
private void BindListView(string DCLookupMstr_Value, int reportCatId, string DCLookup_Value = null)
{
using (Model.OperationalAnalyticsEntities oadb = new Model.OperationalAnalyticsEntities())
{
var res = oadb.prGetDailyCensusLookup(DCLookupMstr_Value, reportCatId, DCLookup_Value);
Session["LookupValues"] = res;
lvLookup.DataSource = res.ToList();
lvLookup.DataBind();
}
}
And I put a search box(textbox) on that listview dialog. 然后在该列表视图对话框上放置一个搜索框(文本框)。 If user type any text/chars, using linq query..populate the listview again with the values which contains given chars.
如果用户键入任何文本/字符,请使用linq query ..再次使用包含给定字符的值填充列表视图。 My code is below
我的代码如下
protected void txtSearch_TextChanged(object sender, EventArgs e)
{
var text = txtSearch.Text;
//var list = new List<Model.prGetDailyCensusLookup_Result>();
var lookUpValue = Session["LookupValues"] as ObjectResult<Model.prGetDailyCensusLookup_Result>;
var list = lookUpValue.Where(x => x.DCLookup_Value.Contains(text));
lvLookup.DataSource = list.ToList();
lvLookup.DataBind();
}
I am getting the "result of a query cannot be enumerated more than once" where ever i added .ToList(). 我在添加.ToList()的地方收到“查询结果不能多次枚举”的信息。 I am not sure what did I miss.
我不确定我错过了什么。
Please Help! 请帮忙!
In BindListView
, when you do .ToList()
, it enumerates the query for the first time. 在
BindListView
,当您执行.ToList()
,它将首次枚举查询。 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. 当您再次在
txtSearch_TextChanged
执行.ToList()
时,它将第二次枚举查询,这是不支持的。
You should store the result of .ToList()
in the session, rather than the query: 您应该将
.ToList()
的结果存储在会话中,而不是查询中:
Session["LookupValues"] = lvLookup.DataSource = res.ToList();
You value you are storing in Session
is the LINQ query, not the result of the query. 您认为您存储在
Session
是LINQ查询,而不是查询结果。 The second time it is used (the list.ToList()
) it throws this error. 第二次使用它(
list.ToList()
)将引发此错误。
This is easily fixed by storing the result as a list in Session
instead. 通过将结果作为列表存储在
Session
可以轻松解决此问题。
var res = oadb.prGetDailyCensusLookup(DCLookupMstr_Value, reportCatId, DCLookup_Value)
.ToList();
Session["LookupValues"] = res;
lvLookup.DataSource = res;
lvLookup.DataBind();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.