How to delete multiple rows using LINQ in linq2db Templates based on two tables?

2.9k Views Asked by At

I've two tables Product and user. Now, i want to delete multiple records at a time with a relation like: i want to delete all the products related to particular user.

I've delete multiple records code in linq2db Templates

using (var db = new DbNorthwind())
{
  db.Product
  .Where(p => p.Discontinued)
  .Delete();
}

But, how to relate that user table to this code?

Source: https://linq2db.github.io/#delete

2

There are 2 best solutions below

0
On BEST ANSWER

You can use the following solution to delete multiple rows using LINQ in linq2db Templates based on two tables:

(
    from p in db.Product
    join u in db.User on ... some join ...
    select p
)
.Delete();
1
On

Try this:

using (var db = new DbNorthwind())
{
  var deletionList=db.Product
                     .Where(p => p.Discontinued).AsEnumerable();
  db.Product.RemoveRange(deletionList);
  db.SaveChanges();
}