vendredi 3 août 2018

MVC List Error List

i'm have a problem and i can't knowing the resons

i'm using foreach to transfer data from list to another but when adding value updated automatically to last value added

ex :

list1 = [1,2,3]
list2 = new List<Model>()

foreach(var item in list1) {
   list2.Add(item)
}
the result in list2 is [ 3, 3, 3]

actually example is below :

#region Sizes
                var _sizes = new List<ProductsSize>();
                var _size = new ProductsSize();

                if (model.Dynamic_ProductsSize.Count > 0)
                {
                    foreach (var item in model.Dynamic_ProductsSize)
                    {
                        _size.SizeId = item;
                        _sizes.Add(_size);
                    }
                }
                model.ProductsSize = _sizes.ToList();

                #endregion

i need to know why take last item and what is the solution for this case plz help me Thank You UW.

Aucun commentaire:

Enregistrer un commentaire