Delete a list of items

Discussion of open issues, suggestions and bugs regarding EntityDAC
Post Reply
elion
Posts: 29
Joined: Wed 10 Dec 2014 07:15

Delete a list of items

Post by elion » Wed 10 Dec 2014 07:39

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!

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Delete a list of items

Post by AlexP » Wed 10 Dec 2014 09:13

No, there is no such a method. You should delete in a loop.

elion
Posts: 29
Joined: Wed 10 Dec 2014 07:15

Re: Delete a list of items

Post by elion » Wed 10 Dec 2014 09:25

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.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Delete a list of items

Post by AlexP » Thu 11 Dec 2014 10:14

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...));

elion
Posts: 29
Joined: Wed 10 Dec 2014 07:15

Re: Delete a list of items

Post by elion » Thu 11 Dec 2014 13:50

Thx! :)

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Delete a list of items

Post by AlexP » Fri 12 Dec 2014 06:47

If you have any further questions, feel free to contact us.

Post Reply