locked
Multiple Remove RRS feed

  • Question

  • User1622294484 posted

    Hey guys ,

    I used to use this line of code , in order to remove a line a database

     ctx.chap.Remove(ctx.chap.First(p => p.titre == btn.CommandArgument));
                            ctx.SaveChanges();

    But this one , removes only one line 

    What I want is to remove all lines where p.titre==btn.CommandArgument

    Any idea how can I change this code to make it work ?

    Thanks

    Saturday, April 6, 2013 9:52 AM

Answers

  • User2117486576 posted

    assuming ctx is a DbContext you might try this:

    ctx.chap.Where(p => p.titre == btn.CommandArguement).ToList().ForEach(o => ctx.chap.Remove(o));

    Haven't tried it but I think it will compile.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 6, 2013 10:14 AM

All replies

  • User-1360095595 posted

    If Remove() will accept a list of records try replacing .First with .Select.

    Saturday, April 6, 2013 10:08 AM
  • User2117486576 posted

    assuming ctx is a DbContext you might try this:

    ctx.chap.Where(p => p.titre == btn.CommandArguement).ToList().ForEach(o => ctx.chap.Remove(o));

    Haven't tried it but I think it will compile.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 6, 2013 10:14 AM