ikey
ikey

Reputation: 331

How to execute an update on multiple rows using Linq Lambda Expression in ASP.Net MVC?

I'm having a hard time trying to execute an update on multiple rows in a table.

I've tried this code:

PayoutEntities payoutdb = new PayoutEntities();

public void SaveStatusBatch(string batchid)
{
   payout_transaction data = payoutdb.payout_transaction.Where(x => x.batchid == batchid).FirstOrDefault();
   data.status = "Success";
   payoutdb.SaveChanges();
}

But it only updates only a single row where the batchid is a match to what is indicated in the parameter. I'm using Èntity Framework in this.

This is how my table looks like:

|batchid|name|status|
|1      |John|      |
|1      |Nick|      |
|1      |Bill|      |
|2      |Ara |      |

I wanted to update the status of John,Nick and Bill to Success. Do you have any suggestions on how am i supposed to do this?

Upvotes: 0

Views: 177

Answers (1)

Md Rahatur Rahman
Md Rahatur Rahman

Reputation: 3244

The concept is to change all the data and then call the SaveChanges of the DBContext object. e.g.:

public void SaveStatusBatch(string batchid)
{
      payout_transaction data = payoutdb.payout_transaction
        .Where(x => x.batchid == batchid)
        .ToList();

      data.ForEach(a=> a.status = "Success");
      payoutdb.SaveChanges();
}

Upvotes: 1

Related Questions