Delet entires in database by date

Posts   
 
    
rparkins
User
Posts: 66
Joined: 04-May-2005
# Posted on: 02-Sep-2005 15:01:55   

Hi,

I am having trouble deleting entries in a table. I want to delete all entries that have a created date of less than 1 minute from now... The code below if what I am using. Is this correct, ie. do I use an Entity to delete and pass the filter across to delete mulitple entries?

Really appreciate the help

Regards

Richard

MessageIdFilterEntity entity = new MessageIdFilterEntity();

//now create the filter IPredicateExpression filter = new PredicateExpression();

filter.Add(new PredicateExpression(PredicateFactory.CompareValue(MessageIdFilterFieldIndex.Created, ComparisonOperator.LessEqual, DateTime.Now.AddMinutes(-1))));

entity.Delete(filter);

rparkins
User
Posts: 66
Joined: 04-May-2005
# Posted on: 02-Sep-2005 16:07:18   

Quick solution, the delete of multiple rows will work via the collection object and applying the filter through this object..

Apologies for the poor spelling was in a rush rage

Paul.Lewis
User
Posts: 147
Joined: 22-Aug-2005
# Posted on: 03-Sep-2005 03:25:27   

You are right.

Here's an excerpt from the User manual:

Generated code - Using the entity collection classes, SelfServicing

Deleting one or more entities from the persistent storage

If you want to delete one or more entities from the persistent storage the same problem as with updating a set of entities appears: you first have to load them into memory, call Delete() and they'll be deleted. To delete a set of entities from the persistent storage directly, you can use DeleteMulti() overloads or the DeleteMultiManyToOne method to achieve your goal. All DeleteMulti() methods work directly on the persistent storage except one, the DeleteMulti() method which does not take any parameters. That one works with the objects inside the collection and deletes them one by one from the persistent storage using an own transaction if the current collection isn't part of an existing transaction. (See for more information about transactions the section Transactions). The DeleteMulti() methods which do accept parameters and thus work on the persistent storage work the same as the UpdateMulti*() methods, except of course the DeleteMulti() methods do not accept an entity with changed fields. See for an example how to filter rows for DeleteMulti() the UpdateMulti() example given earlier on this page.

This code should get the job done!



MessageIdFilterCollection myCollection = new MessageIdFilterCollection();
            
//now create the filter
IPredicateExpression filter = new PredicateExpression();

filter.Add(new PredicateExpression(PredicateFactory.CompareValue(MessageIdFilterFieldIndex.Created, ComparisonOperator.LessEqual, DateTime.Now.AddMinutes(-1))));
            
myCollection.DeleteMulti(filter);