user2067567
user2067567

Reputation: 3813

Delete SQL IN way using Entity Framework

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

Upvotes: 0

Views: 246

Answers (1)

Jan P.
Jan P.

Reputation: 3297

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.

Upvotes: 5

Related Questions