Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查询结果不能枚举多次。_C#_Asp.net_.net_Entity Framework_Linq - Fatal编程技术网

C# 查询结果不能枚举多次。

C# 查询结果不能枚举多次。,c#,asp.net,.net,entity-framework,linq,C#,Asp.net,.net,Entity Framework,Linq,我有一个listview对话框将数据绑定到该listview private void BindListView(string DCLookupMstr_Value, int reportCatId, string DCLookup_Value = null) { using (Model.OperationalAnalyticsEntities oadb = new Model.OperationalAnalyticsEntities())

我有一个listview对话框将数据绑定到该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();
            }            
    }
我在listview对话框上放了一个搜索框(文本框)。如果用户键入任何文本/字符,请使用linq query..再次使用包含给定字符的值填充listview。我的代码在下面

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();  
     }
protectedvoid txtSearch\u text已更改(对象发送方,事件参数e)
{
var text=txtSearch.text;
//var list=新列表();
var lookUpValue=Session[“LookupValues”]作为ObjectResult;
var list=lookUpValue.Where(x=>x.DCLookup_Value.Contains(text));
lvLookup.DataSource=list.ToList();
lvLookup.DataBind();
}
我在添加.ToList()的地方得到了“查询结果不能多次枚举”。我不知道我错过了什么


请帮忙

BindListView
中,当您执行
.ToList()
时,它会第一次枚举查询。会话中存储的是查询本身。当您在
txtSearch\u TextChanged
中再次执行
.ToList()
时,它会再次枚举查询,这是不受支持的

您应该在会话中存储
.ToList()
的结果,而不是在查询中:

Session["LookupValues"] = lvLookup.DataSource = res.ToList();

您在
会话中存储的值是LINQ查询,而不是查询结果。第二次使用时(list.ToList()
),它会抛出此错误

通过将结果存储为
会话
中的列表,可以很容易地解决这一问题

var res = oadb.prGetDailyCensusLookup(DCLookupMstr_Value, reportCatId, DCLookup_Value)
              .ToList();
Session["LookupValues"] = res;
lvLookup.DataSource = res;
lvLookup.DataBind();

错误消息是不言自明的。您尝试多次获取查询结果。你不能那样做。那怎么办?你不明白吗?您还试图执行一个基于已处理数据源的查询,因此这可能也会给您带来问题。