对实体对象执行操作时的性能问题 [英] Performance issue when performing operations on entity objects

查看:59
本文介绍了对实体对象执行操作时的性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码在多个foreach循环中遇到性能问题.首先,我获取了ReturnDetails的列表,然后根据详细信息ID获取了HandlingInfo对象.然后,根据操作值,再次更新ReturnDetail对象.

Im facing performance issue in below code in multiple foreach loops. First im getting a list of ReturnDetails and then based on detail id get the HandlingInfo object. Then based on value of action, update the ReturnsDetail Object again.

加载3000条ReturnDetail记录需要花费一分钟多的时间.在本地调试时,它会运行无数次.

It take more than a minute for loading 3000 records of ReturnsDetail. While debugging locally, it runs for infinite amount of time.

无论如何,请让我知道我可以重构此代码.

Please let me know in anyway i can refactor this code .

感谢您的帮助.

 lstReturnsDetail = dcReturnsService.GetReturnDetailsInfo(header_id);

 List<HandlingInfo> lstHandlingInfo = null;
 foreach (ReturnsDetail oReturnsDetail in lstReturnsDetail)
 {
     using (DCReturns_Entities entities = new DCReturns_Entities())
     {
         lstHandlingInfo = entities.HandlingInfoes.Where(f => f.detail_id == oReturnsDetail.id).ToList();
         if(lstHandlingInfo != null)
         {
             foreach (HandlingInfo oHandlingInfo in lstHandlingInfo)
             {
                 if (oHandlingInfo.action == "DST")
                 {
                     oReturnsDetail.destroy += Convert.ToInt32(oHandlingInfo.qty);
                 }
                 else if (oHandlingInfo.action == "SHP")
                 {
                     oReturnsDetail.to_shop += Convert.ToInt32(oHandlingInfo.qty);
                 }
                 else if (oHandlingInfo.action == "RBX")
                 {
                     oReturnsDetail.in_stock += Convert.ToInt32(oHandlingInfo.qty);
                 }
             }
         }
     }

     oReturnsDetail.received_qty = oReturnsDetail.destroy + oReturnsDetail.to_shop + oReturnsDetail.in_stock;
 }

 dgReturnsDetail.DataSource = lstReturnsDetail.OrderByDescending(g => g.id).ToList();
 Session[DCReturnsConstants.Returns_Detail_Entity] = lstReturnsDetail;
 dgReturnsDetail.DataBind();

推荐答案

这是su-do代码!但是你应该得到jist.

this is su-do code! but you should get the jist.

//modify this to return all of them into mem, and then filter on this... 
//if it can not be done here then do below..
var lstReturnsDetail = dcReturnsService.GetReturnDetailsInfo(header_id);

//then create a list here which fetches all, 
List<[type]> somelist
List<int> listId = lstReturnsDetail.select(x=>x.id).tolist();
using (var db = new DCReturns_Entities())
{
    somelist = db.HandlingInfoes.Where(f =>  listId.Contains(  f.detail_id)).ToList();
}

foreach (ReturnsDetail oReturnsDetail in lstReturnsDetail)
{
    //performance issue is here
    //using (DCReturns_Entities entities = new DCReturns_Entities())
    //{
    //  lstHandlingInfo = entities.HandlingInfoes.Where(f => f.detail_id == oReturnsDetail.id).ToList();
    //} 

    //insead fetach all before, into mem and filter from that list.
    var lstHandlingInfo = somelist.Where(f => f.detail_id == oReturnsDetail.id).ToList();

    //code ommited for reaablity
}
//code ommited for reaablity

这篇关于对实体对象执行操作时的性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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