5

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

Chanikya
  • 476
  • 1
  • 8
  • 22

2 Answers2

8

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();
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
IT.
  • 859
  • 4
  • 11
-2

Try this:

using (var db = new DbNorthwind())
{
  var deletionList=db.Product
                     .Where(p => p.Discontinued).AsEnumerable();
  db.Product.RemoveRange(deletionList);
  db.SaveChanges();
}
Sumit raj
  • 821
  • 1
  • 7
  • 14