Jeeva
Jeeva

Reputation: 4663

Removing duplicate from TStringList

I am parsing a dataset and assigning values to TStringList i want to avoid the duplicates. I use the following code but still duplicates are inserted.

  channelList := TStringList.Create;
  channelList.Duplicates := dupIgnore;
  try
    dataset.First;
    while not dataset.EOF do
    begin
        channelList.Add(dataset.FieldByName('CHANNEL_INT').AsString)  ;
        dataset.Next;
    end;

why does the duplicates added?

Upvotes: 21

Views: 22101

Answers (2)

whosrdaddy
whosrdaddy

Reputation: 11860

Think out of the box and avoid the duplicates up front?

I don't know what DB you are using but for example on SQL server it is just a matter of querying:

'SELECT DISTINCT CHANNEL_INT FROM MYTABLE';

and then you can add the results to your TStringList without being worried about duplicates.

Upvotes: 10

Arioch 'The
Arioch 'The

Reputation: 16045

You did read http://docwiki.embarcadero.com/Libraries/XE2/en/System.Classes.TStringList.Duplicates , didn't you ?

Then you missed the most repeated word there - "sorted"

channelList.Sorted := true

var F: TField;

channelList := TStringList.Create;
channelList.Sorted := True;
channelList.Duplicates := dupIgnore;

try
   dataset.First;
   F := dataset.FieldByName('CHANNEL_INT');
   while not dataset.EOF do
   begin
      channelList.Add(F.AsString);
      dataset.Next;
   end;

Upvotes: 41

Related Questions