Link to home
Start Free TrialLog in
Avatar of Nargzul
Nargzul

asked on

Entity framework: Delete objects that respects some conditions

Hi!

I only want to know if this is possible to delete, with Entity Framework 4.0 some objects directly by specifying conditions.

I know I can select all object and then do a foreach on them and delete them. the goal is to avoid to retrieve data only to delete them. So please don't provide me a solution like

foreach( MyObjectType myObject in DataContext.MyObjectType.Where( <conditionsHere>))
   DataContext.MyObjectType.DeleteObject(myObject);
myObject.SaveChanges();

The goal is to have something like:

DataContext.MyObjectType.Delete.Where(m=> m.date<= someOtherDate);
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nargzul
Nargzul

ASKER

Thank you, but I find this limitation stupid :s
You could execute an SQL comment directly by using the connection of the ObjectContext but it must be a correctly formed SQL syntax because the command is not checked at compile time but only at run-time.

ObjectContextInstance.ExecuteStoreCommand("SQL Statemens as a string", params Object[] parameters);

Documentation ObjectContext.ExecuteStoreCommand Method :
http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.executestorecommand.aspx