如何避免foreach循环 [英] How to avoid the foreach loop

查看:82
本文介绍了如何避免foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在gridview中有一个gridview,有10,000条记录。用户将选择所有10,000并单击确认按钮。我将每行的confirmid更新为Y并将其保存在数据库中。通过使用foreach循环,我正在执行该过程,因此它需要花费大量时间来更新浏览器被冻结或者我得到Timed out错误。如何避免这种情况有没有办法在没有foreach循环的情况下更新行。这是我的代码:



  private   bool 确认()
{
var result = 来自 GridViewRow msgRow in grdPrice.Rows
where ((CheckBox)msgRow.FindControl( chkSelect))。选中
选择 grdPrice.Rows [msgRow.RowIndex];
obj = new ProductMasterInfo();
DataTable dtPrice;
string res = string .Empty;
if (ViewState [ UploadNo]!= null
{
dtPrice = obj.GetEditData( P,ViewState [ UploadNo]的ToString());
foreach (GridViewRow行 结果)
{
string refNo =(row.Cells [iTemp] .FindControl( hfReferenceNo as HiddenField).Value;
DataRow [] dr = dtPrice.Select( ReferenceNo =' + refNo + < span class =code-string>
');
dr [ 0 ] [ ConfirmId ] = Y;
dr [ 0 ] [ UpdateId ] = SessionVarriables.UserIdSession;
dr [ 0 ] [ UpdateDate ] = DateTime.Now.ToString();
}
res = obj.UpdateDataByUploadNo(dtPrice, P,ViewState [ UploadNo]。ToString());
}
else
{
UserUtil.Message( 无数据上传 .Page);
}
return isSaved(res);
}





谢谢

解决方案

简单:不要t在一个页面中显示如此多的信息。



你真的认为这个星球上的任何用户想要去一个网站然后浏览10,000行信息​​,寻找他感兴趣的数据?



如果你翻页,提供搜索和过滤选项,让他看到相关的子集数据,然后你处理时间几乎没有(所以你的网站及时响应),你的用户更高兴,因为他可以在大量的原始数据中找到他想要的东西!


把它放到你的web.config;

< system.web> 
< customerrors mode = Off />
< httpruntime apprequestqueuelimit = 100 executiontimeout = 60000 />
< / system.web >


Hi,

I am having a gridview in that gridview there are 10,000 records are there. The user will select all the 10,000 and click on confirm button. I will update the confirmid for that each row as Y and save it in database. By using the foreach loop I am doing the process so its taking so much of time to update some times the browser gets freezed or else I am getting the Timed out error. How to avoid this situation is there any way to update the row without the foreach loop. Here is my code:

private bool Confirm()
 {
     var result = from GridViewRow msgRow in grdPrice.Rows
                  where ((CheckBox)msgRow.FindControl("chkSelect")).Checked
                  select grdPrice.Rows[msgRow.RowIndex];
     obj = new ProductMasterInfo();
     DataTable dtPrice;
     string res = string.Empty;
     if (ViewState["UploadNo"] != null)
     {
         dtPrice = obj.GetEditData("P", ViewState["UploadNo"].ToString());
         foreach (GridViewRow row in result)
         {
             string refNo = (row.Cells[iTemp].FindControl("hfReferenceNo") as HiddenField).Value;
             DataRow[] dr = dtPrice.Select("ReferenceNo='" + refNo + "'");
             dr[0]["ConfirmId"] = "Y";
             dr[0]["UpdateId"] = SessionVarriables.UserIdSession;
             dr[0]["UpdateDate"] = DateTime.Now.ToString();
         }
          res = obj.UpdateDataByUploadNo(dtPrice, "P", ViewState["UploadNo"].ToString());
     }
     else
     {
         UserUtil.Message("No Data Uploaded", this.Page);
     }
     return isSaved(res);
 }



Thanks

解决方案

Simple: don't display so much information in one page.

Do you really think any user on this planet wants to go to a web site and then trawl through 10,000 rows of information looking for the data he is interested in?

If you page it, provide search and filter options to let him see a relevant subset of the data, then you processing time drops to near nothing (so your site responds in a timely manner) and your user is happier because he can find what he wants in the mass of raw data!


Put this to your web.config;

<system.web>
<customerrors mode="Off" />
<httpruntime apprequestqueuelimit="100" executiontimeout="60000" />
</system.web>


这篇关于如何避免foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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