I have the following code which traverse all data in the TClientDataSet
, my purpose is to delete all records except DocKey=20381
.
But with the following codes, you will notice record with DocKey=20381 being traversed twice (traverse times = 6, which suppose to be 5 times as we only have 5 records in TClientDataSet
).
If we enable this line -> D.IndexFieldNames := 'DocKey'
, then the data will traverse correctly. May I know is this a Delphi bug? Or any way to solve this besides using IndexFieldNames
?
var
D: TClientDataSet;
begin
D := TClientDataSet.Create(Self);
with D do begin
FieldDefs.Add('DocKey', ftInteger);
CreateDataSet;
AppendRecord([20157]);
AppendRecord([20162]);
AppendRecord([20381]);
AppendRecord([20372]);
AppendRecord([20377]);
end;
// D.IndexFieldNames := 'DocKey';
D.First;
while not D.Eof do begin
if D.Fields[0].AsInteger = 20381 then
D.Next
else
D.Delete;
end;
end;