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

3
  • So stackoverflow.com/a/13903501/212121 and using Contains does not help? Commented May 23, 2013 at 6:24
  • It does! but i thoought ll get better way fr my scenario with passing LIST<id> and posted hre ! Commented May 23, 2013 at 6:29
  • This is not a task for LINQ! LINQ is a query language for data selection not for executing commands! Commented May 23, 2013 at 6:52

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The DeleteObject method from entity also can be used. db.DeleteObject(item);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.