locked
Can You Change A Value When Using List.Add Populating From A DataBase RRS feed

  • Question

  • User-224188520 posted

    Hey All,

    I have a situation where I need to loop through a bunch of mixed values in a table and populate a list of items from the results in the first table, not sure if that is very clear.

    Here is my code, followed by what I need to do.

    foreach (var item in items)
    {
        Inventory.Add(
            (from i in db.Inventory
             where i.InventoryID == item.InventoryID
             select i).FirstOrDefault());
    }

    So here is my issue/question: The database inventory records have a value of ItemCount that comes from the table Inventory(obviously) Is there a way that I can change/overwrite the value of ItemCount while I am adding the inventory record to the list? The itemCount is actually coming from the Items list. I only need to change the ItemCount value for the Inventory list, for display purposes in my view.

    I hope that makes sense,

    Any help would be appreciated,

    Regards,

    Sunday, April 16, 2017 1:19 PM

Answers

  • User-224188520 posted

    Well that was easier then I thought, sometimes the answer is staring you in the face, here is my solution.

    foreach (var item in items)
    {
        Inventory.Add(
            (from i in db.Inventory
             where i.InventoryID == item.InventoryID
             select i).FirstOrDefault());
    
        Inventory.Last().ItemCount = item.LineItemCount;
    }

    Regards,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 16, 2017 2:36 PM