【问题标题】:new item adding to List overriding the previous one [duplicate]添加到列表的新项目覆盖前一个[重复]
【发布时间】:2015-10-06 08:17:51
【问题描述】:

我在 C# 中使用List<>。我想在 List 中添加值。但问题是第一项添加成功,但是当第二项将相同的值 ovverride 插入第一项时,例如第一项 abc 插入成功,但是当第二项 xyz 出现时,它 ovveride abcxyz 和这两项显示xyz。这是我的代码。

DataTable dtbl3 = new DataTable();   
List<CartItems> lst = (List<CartItems>)Session["mycart"];
dtbl3 = DAL.Get("Select * from mytable");
List<EmailClass>  lstCstmer = new List<EmailClass>();
for (int j = 0; j < lst.Count; j++)
{          
    emailLst.__EmailcstName = dtbl3.Rows[0]["cstm_name"].ToString();
    emailLst.__EmailcstLName = dtbl3.Rows[0]["cstm_LName"].ToString();
    emailLst.__EmailcstAddress = dtbl3.Rows[0]["cstm_Addr"].ToString();
    emailLst.__EmailcstPhoneNo = dtbl3.Rows[0]["cstm_Phone"].ToString();
    emailLst.__EmailcstCellNo = dtbl3.Rows[0]["cstm_CellNo"].ToString();
    emailLst.__EmailcstskypId = dtbl3.Rows[0]["cstm_skypeId"].ToString();
    emailLst.__EmailcstEmail = dtbl3.Rows[0]["cstm_email"].ToString();
    emailLst.__EmailcstCountry = dtbl3.Rows[0]["cstm_country"].ToString();
    emailLst.__EmailcstCity = dtbl3.Rows[0]["cstm_City"].ToString();
    emailLst.__EmailcstZipcode =Convert.ToInt32( dtbl3.Rows[0]["cstm_ZipCode"].ToString());
    emailLst.__EmailcstRemarks = dtbl3.Rows[0]["cstm_remarks"].ToString();       
    emailLst._EmailCartProdName = lst[j]._CartProdName;
    emailLst._EmailCartProdPrice = lst[j]._CartProdPrice;
    emailLst._EmailCartProdQnty = lst[j]._CartProdQnty;
    emailLst._EmailCartProdCode = lst[j]._CartProdName;
    emailLst._EmailTotalProdPrice = lst[j]._TotalProdPrice;
    lstCstmer.Add(emailLst);           
}

【问题讨论】:

标签: c# asp.net list


【解决方案1】:

您一遍又一遍地添加同一个项目,因为它是一个引用类型,所以列表中的所有条目都指向同一个 EmailClass 实例。

在每次循环迭代中创建一个新实例来解决这个问题:

for (int j = 0; j < lst.Count; j++)
{
    emailLst = new EmailClass();

    emailLst.__EmailcstName = dtbl3.Rows[0]["cstm_name"].ToString();
    emailLst.__EmailcstLName = dtbl3.Rows[0]["cstm_LName"].ToString();
    emailLst.__EmailcstAddress = dtbl3.Rows[0]["cstm_Addr"].ToString();
    emailLst.__EmailcstPhoneNo = dtbl3.Rows[0]["cstm_Phone"].ToString();
    emailLst.__EmailcstCellNo = dtbl3.Rows[0]["cstm_CellNo"].ToString();
    emailLst.__EmailcstskypId = dtbl3.Rows[0]["cstm_skypeId"].ToString();
    emailLst.__EmailcstEmail = dtbl3.Rows[0]["cstm_email"].ToString();
    emailLst.__EmailcstCountry = dtbl3.Rows[0]["cstm_country"].ToString();
    emailLst.__EmailcstCity = dtbl3.Rows[0]["cstm_City"].ToString();
    emailLst.__EmailcstZipcode =Convert.ToInt32( dtbl3.Rows[0]["cstm_ZipCode"].ToString());
    emailLst.__EmailcstRemarks = dtbl3.Rows[0]["cstm_remarks"].ToString();

    emailLst._EmailCartProdName = lst[j]._CartProdName;
    emailLst._EmailCartProdPrice = lst[j]._CartProdPrice;
    emailLst._EmailCartProdQnty = lst[j]._CartProdQnty;
    emailLst._EmailCartProdCode = lst[j]._CartProdName;
    emailLst._EmailTotalProdPrice = lst[j]._TotalProdPrice;

    lstCstmer.Add(emailLst);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多