Home > Software design >  Use Multiple Linq queries in each other
Use Multiple Linq queries in each other

Time:05-01

Hi I use this code for deleting product object from DB

_context.Products.Remove(_context.Products.Find(id));

Is that true ? using Multiple Linq queries in each other ?

CodePudding user response:

Although what you are doing is fine. But it is not the preferred way of removing something from the database. I would suggest you to find the record first and then check if it is found or not. If record exist in the DB, then delete the record.

var result = _context.Products.Find(id);
if(result != null){
_context.Products.Remove(result);
_context.SaveChanges();
}
  • Related