使代码快速执行 - 替换For循环

时间:2016-06-10 10:12:14

标签: c# for-loop foreach

由于for循环实现,执行这些循环需要很多时间

如何更快地替换它,下面的表格也没有太多记录,另​​外我也制作了主键,但for循环仍然很慢

     public List<BusinessLayer.Transactions.CDANumberTracking> GetPOUnusedCDANumberTrackingItems(string code)
    {
        List<BusinessLayer.Transactions.CDANumberTracking> results = new List<BusinessLayer.Transactions.CDANumberTracking>();
        List<Entity.Transactions.CDANumberTracking> SoUsedBagList = new List<Entity.Transactions.CDANumberTracking>();
        List<Entity.Transactions.POCDANumberTracking> rejects = new List<SalesOrderModule.Entity.Transactions.POCDANumberTracking>();
        List<Entity.Transactions.POCDANumberTracking> returns = new List<SalesOrderModule.Entity.Transactions.POCDANumberTracking>();

        List<Entity.Transactions.POCDANumberTracking> rejectList = new List<SalesOrderModule.Entity.Transactions.POCDANumberTracking>();
        List<Entity.Transactions.POCDANumberTracking> returnRejectList = new List<SalesOrderModule.Entity.Transactions.POCDANumberTracking>();
        List<Entity.Transactions.POCDANumberTracking> SearchList = new List<SalesOrderModule.Entity.Transactions.POCDANumberTracking>();

        try
        {
            if (!InOpenLookup)
                (Connection as SQL).BeginTransaction();

            DataLayer.Tables.PLSPOCDANumberTrackingDNew sampleTable = new SalesOrderModule.DataLayer.Tables.PLSPOCDANumberTrackingDNew(this.Connection);
            sampleTable.SearchCriteria[0].Value = code.Trim();
            sampleTable.SearchCriteria[1].Value = (int)0;
            List<Entity.Transactions.POCDANumberTracking> results1 = sampleTable.Reads(false);

            if (results1.Count > 0)
            {
                rejectList.AddRange(results1);
            }

            DataLayer.Tables.PLSPOCDANumberTrackingReturnD sampleTable2 = new SalesOrderModule.DataLayer.Tables.PLSPOCDANumberTrackingReturnD(this.Connection);
            sampleTable2.SearchCriteria[0].Value = code.Trim();
            List<Entity.Transactions.POCDANumberTracking> results2 = sampleTable2.Reads(false);

            if (results2.Count > 0)
            {
                returnRejectList.AddRange(results2);
            }

            DataLayer.Tables.PLSPOCDANumberTrackingD sampleTable3 = new SalesOrderModule.DataLayer.Tables.PLSPOCDANumberTrackingD(this.Connection);
            sampleTable3.SearchCriteria[0].Value = code.Trim();
            SearchList = sampleTable3.Reads(false);

            DataLayer.Tables.PSOMCDANumberTrackingD sampleTable4 = new SalesOrderModule.DataLayer.Tables.PSOMCDANumberTrackingD(this.Connection, null);
            sampleTable4.SearchCriteria[3].Value = code.Trim();
            sampleTable4.SearchCriteria[6].Value = false;
            SoUsedBagList = sampleTable4.Read(false);

            //process data...

            Entity.Transactions.POCDANumberTracking temp;
            foreach (Entity.Transactions.POCDANumberTracking rejectItem in rejectList)
            {
                for (int i = rejectItem.From; i <= rejectItem.To; i++)
                {
                    temp = new SalesOrderModule.Entity.Transactions.POCDANumberTracking();
                    temp.From = i;
                    temp.To = i;
                    temp.Code = rejectItem.Code.Trim();
                    temp.GrnNo = rejectItem.GrnNo.Trim();
                    temp.WbcNo = rejectItem.WbcNo.Trim();
                    rejects.Add(temp);
                }
            }
            //returns

            foreach (Entity.Transactions.POCDANumberTracking returnItem in returnRejectList)
            {
                for (int i = returnItem.From; i <= returnItem.To; i++)
                {
                    temp = new SalesOrderModule.Entity.Transactions.POCDANumberTracking();
                    temp.From = i;
                    temp.To = i;
                    temp.Code = returnItem.Code.Trim();
                    temp.GrnNo = returnItem.GrnNo.Trim();
                    temp.WbcNo = returnItem.WbcNo.Trim();
                    returns.Add(temp);
                }
            }


            Entity.Transactions.CDANumberTracking temp2;
            Entity.Transactions.CDANumberTracking temp3;
            Entity.Transactions.POCDANumberTracking temp4;
            foreach (Entity.Transactions.POCDANumberTracking searchItem in SearchList)
            {
                for (int i = searchItem.From; i <= searchItem.To; i++)
                {
                    temp = null;
                    temp3 = null;
                    temp4 = null;
                    //check if the bag is on reject list
                    temp = rejects.Find(delegate(Entity.Transactions.POCDANumberTracking tc) { return (tc.From == i && tc.WbcNo.Trim().ToUpper() == searchItem.WbcNo.Trim().ToUpper() && tc.GrnNo.Trim().ToUpper() == searchItem.GrnNo.Trim().ToUpper()); });
                    if (temp != null)
                        continue;

                    //check if the bag is on return list
                    temp4 = returns.Find(delegate(Entity.Transactions.POCDANumberTracking tcc) { return (tcc.From == i && tcc.GrnNo.Trim().ToUpper() == searchItem.GrnNo.Trim().ToUpper()); });
                    if (temp4 != null)
                        continue;

                    //check if the bag is alredy used in So module...
                    temp3 = SoUsedBagList.Find(delegate(Entity.Transactions.CDANumberTracking cda) { return (cda.Code.Trim().ToUpper() == searchItem.Code.Trim().ToUpper() && cda.BagNo == searchItem.From); });
                    if (temp3 != null)
                        continue;


                    temp2 = new SalesOrderModule.Entity.Transactions.CDANumberTracking();
                    temp2.BagNo = i;
                    temp2.Code = searchItem.Code.Trim();
                    temp2.LineNo = 0;
                    temp2.Location = string.Empty;
                    temp2.WbcNo = string.Empty;
                    temp2.ID = null;
                    temp2.IsReturned = false;
                    temp2.IsSelected = false;
                    temp2.ItemNo = string.Empty;
                    temp2.Status = SalesOrderModule.Entity.ModifyStatus.New;

                    results.Add(BusinessLayer.Transactions.CDANumberTracking.GetCDANumberTracking(this, temp2, null));
                }
            }

            if (!InOpenLookup)
                (Connection as SQL).EndTransaction();
        }
        catch (Exception er)
        {
            if (!InOpenLookup)
                (Connection as SQL).Rollback();
            throw er;
        }
        return results;
    }

每个需要放置的第二个for循环......需要一些帮助

2 个答案:

答案 0 :(得分:1)

你应该尽可能地将内循环分解出来。正如代码现在所示,您不必重复以下操作:

returnItem.Code.Trim();
returnItem.GrnNo.Trim();
returnItem.WbcNo.Trim();

我没有足够的信息来判断这是否会对性能产生任何影响。

其他嫌犯是new SalesOrderModule.Entity.Transactions.POCDANumberTracking()returns.Add(temp)。如果returns是某种有序列表,那么这可能会带来相当大的性能损失。如果它只是一个简单的List那么它就不应该,并且你无论如何都无法改进它。 关于构造函数,只有你知道它有多贵,但你也无法避免它。

所有这些,你的代码看起来像这样:

Entity.Transactions.POCDANumberTracking temp;

foreach (Entity.Transactions.POCDANumberTracking returnItem in returnRejectList)
{
    var code = returnItem.Code.Trim();
    var grnNo = returnItem.GrnNo.Trim();
    var wbcNo = returnItem.WbcNo.Trim();

    for (int i = returnItem.From; i <= returnItem.To; i++)
    {
        temp = new SalesOrderModule.Entity.Transactions.POCDANumberTracking();
        temp.From = i;
        temp.To = i;
        temp.Code = code;
        temp.GrnNo = grnNo;
        temp.WbcNo = wbcNo;
        returns.Add(temp);
     }
 }

答案 1 :(得分:0)

我必须限制foreach循环下的条目,这是调试代码的最佳方法

所以数据层代码在

中引用
sampleTable.Reads(false);
sampleTable2.Reads(false);
 sampleTable3.Reads(false);
 sampleTable4.Reads(false);

需要通过包含搜索项(我的意思是SQL语句)来修改