Reputation: 511
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. 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(). I am not sure what did I miss.
Please Help!
Upvotes: 0
Views: 2186
Reputation: 5480
You value you are storing in Session
is the LINQ query, not the result of the query. The second time it is used (the list.ToList()
) it throws this error.
This is easily fixed by storing the result as a list in Session
instead.
var res = oadb.prGetDailyCensusLookup(DCLookupMstr_Value, reportCatId, DCLookup_Value)
.ToList();
Session["LookupValues"] = res;
lvLookup.DataSource = res;
lvLookup.DataBind();
Upvotes: 0
Reputation: 292425
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();
Upvotes: 2