查询结果不能被多次枚举错误 [英] the query results cannot be enumerated more than once error

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

问题描述

try
        {
            RealEstateDataClassesDataContext db = new RealEstateDataClassesDataContext();
            if (ddlstate.SelectedItem.Text != "Select State" && dp_city.SelectedItem.Text == "Select City")
            {
                var query = db.getagentdetailsbystate(dp_usertype.SelectedItem.Text, int.Parse(ddlstate.SelectedItem.Value));
                if (query.Count()>0)
                {
                    GridView1.DataSource = query;
                    GridView1.DataBind();
                    GridView1.Visible = true;
                    lb_nodata.Visible = false;
                }
                 
            }
            else if (ddlstate.SelectedItem.Text != "Select State" && dp_city.SelectedItem.Text != "Select City")
            {
                var query1 = db.getagentdetails1(dp_usertype.SelectedItem.Text, int.Parse(ddlstate.SelectedItem.Value), int.Parse(dp_city.SelectedItem.Value));
                if (query1 .Count() > 0)
                {
                    GridView2.DataSource = query1;
                    GridView2.DataBind();
                    GridView1.Visible = false;
                    lb_nodata.Visible = false;
                }
                else
                {
                    lb_nodata.Visible = true;
                    lb_nodata.Text = "Data Is not Available";
                }
                
            }
            else
            {
                lb_nodata.Visible = true;
                lb_nodata.Text = "Data Is not Available";
            }

             
        }



在这里,我得到一个错误,即不能多次枚举查询结果.问题出在哪里?
有人可以解决吗?

在此先感谢!



Here I got the error that the query results cannot be enumerated more than once. Where is the problem?
Can anybody solve that?

Thanks in advance!

推荐答案

很难说为什么不看所有代码.但是,我想原因是这样的:

在该行

It''s pretty tough to say why without seeing all of your code. But, I guess the reason is this:

In the line

var query = db.getagentdetailsbystate(dp_usertype.SelectedItem.Text, int.Parse(ddlstate.SelectedItem.Value));



调用
时,db.getagentdetailsbystate可能会返回 IEnumerable< T>



db.getagentdetailsbystate probably returns an IEnumerable<T>, When you call

query.Count()



您已经列举了您的结果.然后,您将其分配为GridView1的数据源,GridView1再次枚举结果,从而导致此错误.要解决此问题,只需将db.getagentdetailsbystate的结果转换为如下所示的列表:



you have enumerated your results. Then you are assigning this as a data source to GridView1, which enumerates the results again thereby causing this error. To resolve this just convert the results from db.getagentdetailsbystate to a list as shown below:

var query = db.getagentdetailsbystate(dp_usertype.SelectedItem.Text,int.Parse(ddlstate.SelectedItem.Value)).ToList();



同样,对于query1,将其转换为列表,如前所示.那应该可以解决您的问题...

希望这会有所帮助!



Similarly for query1 convert it to a list as shown before. That should resolve your problems...

Hope this helps!


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

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