C # Forehead keeps adding the same item to the list
I am having problems with my simple foreach. I am trying to get data from a database into my list.
IList<DeliveredTaskModel> deliveredTaskModel = new List<DeliveredTaskModel>();
// lines of code
if (materialUsed.Count > 0)
{
foreach (var material in materialUsed)
{
var deliveryModel = new DeliveredTaskModel();
deliveryModel.Info = materialUsed[0].SubPartCode;
deliveryModel.Description = materialUsed[0].Description;
deliveryModel.Qty = materialUsed[0].Qty;
deliveredTaskModel.Add(deliveryModel);
}
}
when i set a breakpoint on foreach. I can see that it has 4 different items in materialUsed. however, when I do this foreach it just adds 4x the same item to the grid.
I am assuming it keeps adding the same item, but why? Can someone please explain?
source to share
You always get access at index zero. Correction options:
If you are using
foreach
, use:foreach (var material in materialUsed) { var deliveryModel = new DeliveredTaskModel(); deliveryModel.Info = material.SubPartCode; deliveryModel.Description = material.Description; deliveryModel.Qty = material.Qty; deliveredTaskModel.Add(deliveryModel); }
If you are using an indexer, change to
for-loop
:for(int i = 0; i < materialUsed.Count, i++) { var deliveryModel = new DeliveredTaskModel(); deliveryModel.Info = materialUsed[i].SubPartCode; deliveryModel.Description = materialUsed[i].Description; deliveryModel.Qty = materialUsed[i].Qty; deliveredTaskModel.Add(deliveryModel); }
Then a custom property initializer would be better:
foreach (var material in materialUsed) { deliveredTaskModel.Add(new DeliveredTaskModel { Info = material.SubPartCode, Description = material.Description, Qty = material.Qty }); }
And then using linq you can achieve this with
.Select
var deliveredTaskModel = materialUsed.Select(material => new DeliveredTaskModel { Info = material.SubPartCode, Description = material.Description, Qty = material.Qty }).ToList();
I suggest you go with the last option :)
Last comment - your if statement (materialUsed.Count > 0)
is redundant because if the collection is empty it won't go into the loop
source to share
You should use the current item in each iteration of the foreach loop instead of referencing the list. Try the following:
IList<DeliveredTaskModel> deliveredTaskModel = new List<DeliveredTaskModel>();
if (materialUsed.Count > 0)
{
foreach (var material in materialUsed)
{
var deliveryModel = new DeliveredTaskModel();
deliveryModel.Info = material .SubPartCode;
deliveryModel.Description = material .Description;
deliveryModel.Qty = material .Qty;
deliveredTaskModel.Add(deliveryModel);
}
}
source to share
IList<DeliveredTaskModel> deliveredTaskModel = new List<DeliveredTaskModel>();
// lines of code
if (materialUsed.Count > 0)
{
foreach (var material in materialUsed)
{
var deliveryModel = new DeliveredTaskModel();
deliveryModel.Info = material.SubPartCode;
deliveryModel.Description = material.Description;
deliveryModel.Qty = material.Qty;
deliveredTaskModel.Add(deliveryModel);
}
}
materialUsed [0] is the first item in your list, and if the number of items you always make in the list is the first you should take the current item "material"
source to share