0

I'm trying to build method that would delete all entries in specified table that match the condition.

    public void DeleteAll(Expression<Func<T, bool>> condition)
    {
        var service = PluralizationService.CreateService(new CultureInfo("en-US"));
        var tableName = service.Pluralize(typeof(T).Name);
        Context.Database.ExecuteSqlCommand(string.Format("DELETE FROM {0} WHERE {1}", tableName, condition));
    }

Currently this is not working as I'm not able to properly convert condition given as lambda expression to meaningful SQL syntax that would fit after WHERE clause. I can do some things manually but I was wondering if there is a way to do it with means provided with .NET framework.

Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
Admir Tuzović
  • 10,997
  • 7
  • 35
  • 71
  • Looks like you're headed down a dangerous path - building dynamic SQL, using a service to guess your table names, and trying to partially build your WHERE clause. I'd say just take a step back and rethink this. Either delete them one at a time using built-in EF functions, or if you're looking for better performance, build some stored procs (or properly prepared and parameterized statements in your code) and call them manually. It may be a little more code to write, but it will be much easier to read, debug, and troubleshoot. – Joe Enos Oct 27 '12 at 20:38
  • 1
    Have a look at http://stackoverflow.com/questions/869209/bulk-deleting-in-linq-to-entities/870081#870081 – Laurence Oct 27 '12 at 21:23

0 Answers0