.net ef-code-first entity-framework extension-methods sql

EF Code First Delete Batch From IQueryable?

I know this is possible in LINQ-to-SQL, and I’ve seen bits and pieces that lead me to believe it’s possible in EF. Is there an extension out there that can do something like this:

var peopleQuery = Context.People.Where(p => p.Name == "Jim");

Where DeleteBatch just picks apart the peopleQuery and creates a single SQL statement to delete all the appropriate records, then executes the query directly instead of marking all those entities for deletion and having it do them one by one. I thought I found something like that in the code below, but it fails immediately because instance can’t be casted to ObjectSet. Does anyone know how to fix this up to work with EF Code First? Or know of anywhere that has an example of this being done?

public static IQueryable<T> DeleteBatch<T>(this IQueryable<T> instance) where T : class
ObjectSet<T> query = instance as ObjectSet<T>;
ObjectContext context = query.Context;
string sqlClause = GetClause<T>(instance);
context.ExecuteStoreCommand("DELETE {0}", sqlClause);
return instance;
public static string GetClause<T>(this IQueryable<T> clause) where T : class
string snippet = "FROM [dbo].[";
string sql = ((ObjectQuery<T>)clause).ToTraceString();
string sqlFirstPart = sql.Substring(sql.IndexOf(snippet));
sqlFirstPart = sqlFirstPart.Replace("AS [Extent1]", "");
sqlFirstPart = sqlFirstPart.Replace("[Extent1].", "");
return sqlFirstPart;