Home > Net >  Modifying two values at sametime using foreach
Modifying two values at sametime using foreach

Time:08-26

Can we do two field modification for a record in a database via enitity framework using foreach? Below method does not work, can anyone suggest different method?

using (_context)
{
    var temp = _context.orders.Where(x => x.emailId == emailId).ToList();
    temp.ForEach(a => a.purchased = true, b => b.purchaseDT = DateTime.Now);
    _context.SaveChanges();
}

CodePudding user response:

This should work for you:

using (_context)
{
    var temp = _context.orders.Where(x => x.emailId == emailId).ToList();
    temp.ForEach(a => 
    { 
        a.purchased = true; 
        a.purchaseDT = DateTime.Now;
    });
    _context.SaveChanges();
}
  • Related