2

I want to iterate through each object in a collection and then update a property on each object with another array collection

My use case is I have some entities which I initially read from my collection into an array, and perform some complex calculation and then update back my original collection.

public void WhatIfCalculation(string Product, string SiteID, System.Linq.IQueryable<DAL.OMS_StockStatus> prodStatus, ref System.Linq.IQueryable<PivotViewModel> activityInfo)
{
    var sff = activityInfo.Where(a => a.Activity == "System Forecast" && a.SiteID == SiteID).ToList();

    aSFF = new double?[sff.Count()];
    for (int i = 0; i < sff.Count(); i++)
    {
        aSFF[i] = sff.ElementAt(i).Value + 1;
    }

    var prf = activityInfo.Where(a => a.Activity == "Planned Receipts" && a.SiteID == SiteID).ToList();
    aPRF = new double?[prf.Count()];
    for (int i = 0; i < prf.Count(); i++)
    {
        aPRF[i] = prf.ElementAt(i).Value + 2;
    }

    // Will perform some calculation here. And then update back my collection.

    for (int i = 0; i < aSFF.Length; i++)
    {
        activityInfo.Where(a => a.Activity == "System Forecast" && a.SiteID == SiteID).ToList().ElementAt(i).Value = aSFF[i];
    }

    for (int i = 0; i < aPRF.Length; i++)
    {
        activityInfo.Where(a => a.Activity == "Planned Receipts" && a.SiteID == SiteID).ToList().ElementAt(i).Value = aPRF[i];
    }
}

But above update is not updating my collection. When I browse back through my activityInfo - its still showing original value.

Ocelot20
  • 10,510
  • 11
  • 55
  • 96
Randeep Singh
  • 998
  • 2
  • 11
  • 31
  • 6
    .ToList() does not mutate the value of the original array. When you .ToList() the activityinfo, you're creating a new list object, updating the new list object, but since you're not putting it anywhere, it just disappears. – C Bauer Sep 17 '14 at 19:24
  • Hope this helps. http://stackoverflow.com/questions/398871/update-all-objects-in-a-collection-using-linq – tdbeckett Sep 17 '14 at 19:28
  • Hope this helps. http://stackoverflow.com/questions/14729239/i-have-list-of-objects-how-update-only-single-item-using-linq-in-c-wich-has-pr – tdbeckett Sep 17 '14 at 19:28
  • Hope this helps. http://stackoverflow.com/questions/18260994/update-property-in-object-collection-with-linq – tdbeckett Sep 17 '14 at 19:29
  • Not only the ToList part you need to rethink. With an order by I am not sure ElementAt(i) is what you think it is. – paparazzo Sep 17 '14 at 20:20
  • Blam - Could you please share more on this concern? Thanks. – Randeep Singh Sep 18 '14 at 07:09
  • I had look into above examples - but they either update generic value to all records or I find them memory consuming doing ForEach update for each iteration. – Randeep Singh Sep 18 '14 at 07:12

1 Answers1

1

Try the following:

public void WhatIfCalculation(string Product, string SiteID, List<DAL.OMS_StockStatus> prodStatus, List<PivotViewModel> activityInfo)
{
    var sff = activityInfo.Where(a => a.Activity == "System Forecast"); // Do this in the calling function >> .Where(a => a.SiteID == SiteID).ToList();

    aSFF = new double?[sff.Count()];
    for (var i = 0; i < sff.Count(); i++)
    {
        aSFF[i] = sff.ElementAt(i).Value + 1;
    }

    var prf = activityInfo.Where(a => a.Activity == "Planned Receipts");
    aPRF = new double?[prf.Count()];
    for (var i = 0; i < prf.Count(); i++)
    {
        aPRF[i] = prf.ElementAt(i).Value + 2;
    }

    // Will perform some calculation here. And then update back my collection.

    for (var i = 0; i < aSFF.Length; i++)
    {
        sff.ElementAt(i).Value = aSFF[i];
    }

    for (var i = 0; i < aPRF.Length; i++)
    {
        prf.ElementAt(i).Value = aPRF[i];
    }
}

As you can see I am sending a list instead of an IQueryable to the method. Reason is that the IQueryable is in fact an query statement and not the record itself.

Basically what you are doing is sending a query to the method which you than use to select the items and change them. The actual items are not changed.

Using the below example you are sending a list of the actual items. The items can than be changed. I also removed some double select statements from your method to enhance the speed.

this should also be useful: Differences between IQueryable, List, IEnumerator?

Community
  • 1
  • 1
Kevin Hendricks
  • 785
  • 1
  • 8
  • 36
  • 1
    Hi @Kevin, my activity info here is actually custom view model and not tied to the data context. I'm actually loading multiple tables into a Pivot model and performing above calculation. Once user confirm the calculation is acceptable, then only I suppose to update my data context. – Randeep Singh Sep 18 '14 at 07:15