20

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?

1
  • 1
    BTW dataset.FieldByName('CHANNEL_INT') is slow unsorted operation. Cache it into the var F: TField before the loop start Jul 16, 2013 at 9:01

2 Answers 2

39

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;
5
  • 2
    +1 for the answer, even the user is not aware/care about the scalability :)
    – RBA
    Jul 16, 2013 at 9:06
  • @RBA well, i think the user with 2K+ karma should at least know, if not care. What about .Sorted miss... well... we all keep makinf the most stupid mistakes once and again despite any experience gained before Perhaps it was just his bad day. :-) Jul 16, 2013 at 9:08
  • Probably that kind of day :)
    – RBA
    Jul 16, 2013 at 9:10
  • @RBA OTOH that makes me wonder what is the datasat ? kind of ISAM ? JSON/XML ? cause the 1st instinct to me would be adding one SQL term to the query so that the dataset just could not contain those duplicates in the 1st Jul 16, 2013 at 9:10
  • 2
    @Arioch'The: oh i am sorry, i am new to Delphi my 2k+ is on c++
    – Jeeva
    Jul 16, 2013 at 11:12
9

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.

7
  • 2
    Why would one fetch many results and filter them out on the client side. If those filtered records are ignored anywhere else in the application, there's no need to fetch them and this is the most correct answer. [+1]
    – TLama
    Jul 16, 2013 at 9:12
  • 1
    @TLama if that is the query to fill the list, yes. But it might be the query with a different goal and getting list of different values is just - for example - for grid auto-filter. Jul 16, 2013 at 9:20
  • 1
    @Arioch, that's what I meant - If those filtered records are ignored anywhere else in the application, there's no need to fetch them...
    – TLama
    Jul 16, 2013 at 9:21
  • 1
    @Arioch, well, I said it wrong. It's not the most correct answer, it's (probably) the best suggestion.
    – TLama
    Jul 16, 2013 at 10:25
  • 3
    I agree this is not an answer to his question, so Arioch deserves the accept here...
    – whosrdaddy
    Jul 16, 2013 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.