Page 1 of 1

Delete a list of items

Posted: Wed 10 Dec 2014 07:39
by elion
Hi there!

How can I delete a list of items from a database table? I would like to define a list of items and remove it at once, see the code below:

Code: Select all

var
  ListOfEntities: TList<TSomeEntity>;
  SomeEntity: TSomeEntity;
  SomeContext: TSomeContext;
begin
  ListOfEntities.Add(TSomeEntity.Create([1]));
  ListOfEntities.Add(TSomeEntity.Create([2]));
  ListOfEntities.Add(TSomeEntity.Create([3]));

  SomeContext.DeleteAndSave(ListOfEntities);
end;
Thank you!

Re: Delete a list of items

Posted: Wed 10 Dec 2014 09:13
by AlexP
No, there is no such a method. You should delete in a loop.

Re: Delete a list of items

Posted: Wed 10 Dec 2014 09:25
by elion
Are you going to support such functionality like this?
What do you think, does it make sense to built it in?

For each ID an extra SQL is executed in a loop, and it takes much more time to delete for example 100 000 items for a table than to execute a query like "delete from <sometable> where IDs in (1,2,3,4,..)". It's a preformance issue.

Re: Delete a list of items

Posted: Thu 11 Dec 2014 10:14
by AlexP
To delete several records, you can execute the following SQL query in the ExecuteSQL method:

Code: Select all

EntityConnection.ExecuteSQL('DELETE FROM TABLE WHERE FIELD IN (1,2,3,4...));

Re: Delete a list of items

Posted: Thu 11 Dec 2014 13:50
by elion
Thx! :)

Re: Delete a list of items

Posted: Fri 12 Dec 2014 06:47
by AlexP
If you have any further questions, feel free to contact us.