如何在C#中添加在各自循环中定义的两个列表数据

时间:2015-02-07 08:53:36

标签: c# asp.net-mvc

    public bool AddPosPromotionalMaster(FormCollection frm, POSPromotionalMaster posprom)
    {
        var promitemcode = frm["PItemCode"].Split(',');
        var promuom = frm["PUom"].Split(',');
        var salerate = frm["SaleRate"].Split(',');
        var discqty = frm["Discount"].Split(',');
         var discpercent = frm["DiscountPercentage"].Split(',');
        var discprice = frm["DiscountPrice"].Split(',');
        var poscode = frm["PosCode"].Split(',');
        var days = frm["WDay"].Split(',');
        var fromtime = frm["FromTime"].Split(',');
        var totime = frm["ToTime"].Split(',');

        var itemcodelength = promitemcode.Length;
        var poscodelength = poscode.Length;
        var dayslength = days.Length;

        var posprommasters = new POSPromotionalMaster();
        List<POSPromotionalMaster> finallist = new List<POSPromotionalMaster>();

        for (int p = 0; p < poscodelength; p++)
        {

            for (int d = 0; d < dayslength; d++)
            {


                for (int i = 0; i < itemcodelength; i++ )
                {
        List<POSPromotionalMaster> pospromlist = new List<POSPromotionalMaster>();

                    posprommasters.PromCode = posprom.PromCode;
                    posprommasters.PromDate = posprom.PromDate;
                    posprommasters.PromDesc = posprom.PromDesc;
                    posprommasters.Type = posprom.Type;
                    posprommasters.BasedItemCode = posprom.BasedItemCode;
                    posprommasters.PItemDesc = posprom.PItemDesc;
                    posprommasters.BasedUom = posprom.BasedUom;
                    posprommasters.PItemCode = promitemcode[i];
                    posprommasters.PUom = promuom[i];
                    posprommasters.SaleRate = Convert.ToDecimal(salerate[i]);
                    posprommasters.Discount = Convert.ToDecimal(discqty[i]);
                    posprommasters.DiscountPrice = Convert.ToDecimal(discprice[i]);
                    posprommasters.FromDate = posprom.FromDate;
                    posprommasters.ToDate = posprom.ToDate;
                    posprommasters.PosCode = poscode[p];
                    posprommasters.WDay = days[d];
                    posprommasters.FromTime = Convert.ToDateTime(fromtime[d]);
                    posprommasters.ToTime = Convert.ToDateTime(totime[d]);
                    posprommasters.CreatedBy = posprom.CreatedBy;
                    posprommasters.CreatedOn = posprom.CreatedOn;
                    posprommasters.CheckedBy = posprom.CheckedBy;
                    posprommasters.CheckedOn = posprom.CheckedOn;
                    posprommasters.AuthorizedBy = posprom.AuthorizedBy;
                    posprommasters.AuthorizedOn = posprom.AuthorizedOn;
                    posprommasters.CheckerComment = posprom.CheckerComment;
                    posprommasters.AuthorizedComment = posprom.AuthorizedComment;
                    posprommasters.DiscountPercentage = Convert.ToDecimal(discpercent[i]);
                    pospromlist.Add(posprommasters);

                }


            }


        }


        bool flg = pospromdal.AddPosPromotional(pospromlist);
        return flg;

    }

这是我的代码,我想将pospromlist数据添加到finallist。 由于pospromlist是在循环内定义的,因此我无法将数据添加到其外部定义的列表中。

1 个答案:

答案 0 :(得分:2)

您需要将posprommasters的声明移至您定义pospromlist的位置,并完全删除pospromlist,将其替换为finallist

需要在每次迭代时定义和创建

posprommasters;否则,您最终会将列表中的所有项目彼此相同。

List<POSPromotionalMaster> finallist = new List<POSPromotionalMaster>();
for (int p = 0; p < poscodelength; p++) {
    for (int d = 0; d < dayslength; d++) {
        for (int i = 0; i < itemcodelength; i++ ) {
            var posprommasters = new POSPromotionalMaster();
            ...
            finallist.Add(posprommasters);
        }
    }
}