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
是在循环内定义的,因此我无法将数据添加到其外部定义的列表中。
答案 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);
}
}
}