0

I am trying to delete multiple rows in EF5.

closer i found is How do I delete multiple rows in Entity Framework (without foreach)

Am struck on how to proceed on this as i need to use LINQ(SQL IN)Version and i dont want to execute SQL.

I have a List of ID's and i need to pass it to IN. How do i implement in LINQ to Entities(EF)

SQL Version of what am tryin to do:

DELETE FROM TEST WHERE TESTID IN (2,3,4,5)

and i need to pass IN parameters from LIST<ID>.

What is the best way to implement this in EF?

Thanks

Community
  • 1
  • 1
user2067567
  • 3,695
  • 15
  • 49
  • 77

1 Answers1

5

You have to use a loop. Your problem is just a cosmetic one, because after using:

var foo =
    from item in db.Test
    where idlist.Contains(item.TESTID)
    select item;

foreach (var item in foo)
{
    db.Entry(item).State = EntityState.Deleted;
}

db.SaveChanges();

the EF will create someting like

DELETE FROM TEST WHERE TESTID IN (2,3,4,5)

for you.

Jan P.
  • 3,261
  • 19
  • 26