1

I have what I think is a simple situation - but I've been stuck on it for a while now.

I am simply querying the database, and putting the results into a viewmodel: CallVM - that part works fine.

What I then want to do, it loop through the QueueByTeam object, and update one of the properties - however, the "looping" part, doesn't save the changes to the QueueByTeam object, so when I return the object to the View, my updates have been ignored:

  var QueueByTeam = db.Calls.Where(x => x.assignedteam == id)
         .Select(call => new CallVM
          {
              customer = call.customer,
              nexttargetdate = call.nexttargetdate
              owner = "";
          });


        foreach (var calls in QueueByTeam)
        {
            calls.owner = "--------";
        }
        // at this point, QueueByTeam has ignored changing the `owner` field to "-------"           
        return View(QueueByTeam.ToList());

Do I need to do something after the foreach loop to save the changes, before returning to the View?

Thanks, Mark

4
  • 1
    What happens if you put the ToList() statement on the end of the select statement. This would force the read of the database and store it locally? Commented May 30, 2013 at 7:22
  • 2
    Could you not put calls.owner = "-----" in place of owner = ""`? Commented May 30, 2013 at 7:23
  • @bob-vale - thank you, that's exactly what the problem was. Can you make that an answer, and I'll mark it as such. Thanks too shahkalpesh for helping. Mark Commented May 30, 2013 at 7:28
  • 1
    Just to clarify, the main point is call => new CallVM. Your query creates new unattached objects and the query is executed 2x. Commented May 30, 2013 at 7:37

1 Answer 1

2

Change the code to:

 var QueueByTeam = db.Calls.Where(x => x.assignedteam == id)
     .Select(call => new CallVM
      {
          customer = call.customer,
          nexttargetdate = call.nexttargetdate
          owner = "";
      })
      .ToList();


    foreach (var calls in QueueByTeam)
    {
        calls.owner = "--------";
    }
    // at this point, QueueByTeam has ignored changing the `owner` field to "-------"           
    return View(QueueByTeam);

i.e. Put the ToList() straight after the Select, before you try and change the data. This forces the database query to run immediately and stores the result in a list.

From the looks of it every time you queried your QueuesByTeam it was requerying the database hence losing your changes.

As a side note, if the change is just to set the owner to "-----" you could put that straight into the original select statement instead of having a seperate for loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.