简体   繁体   中英

how to add the two list data defined in their respective loop in C#

    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;

    }

This is my Code and I want to add the pospromlist data to the finallist . Since the pospromlist is defined inside the loop that's why I can't able to add the data into its externally defined list.

You need to move the declaration of posprommasters to where you define pospromlist , and remove pospromlist altogether, replacing it with finallist .

posprommasters needs to be defined and created on each iteration; otherwise, you would end up with all items of your list being identical to each other.

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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM