More about enumerators: https://www.thedelphigeek.com/2007/03/fun-with-enumerators.html
unit DataSet.Enumerator; interface uses DB; { Example: var t: TDataSet; begin for t in memTable1 do ShowMessage(t.FieldByName('Name').AsString + ' ' + t.FieldByName('Email').AsString); end; } type TDataSetEnumerator = class private FDataSet: TDataSet; FDoFirst: boolean; public constructor Create(ADataSet: TDataSet); function GetCurrent: TDataSet; function MoveNext: boolean; property Current: TDataSet read GetCurrent; end; TDataSetHelper = class helper for TDataSet public function GetEnumerator: TDataSetEnumerator; end; implementation { TDataSetEnumerator } constructor TDataSetEnumerator.Create(ADataSet: TDataSet); begin FDoFirst := True; FDataSet := ADataSet; FDataSet.Open; end; function TDataSetEnumerator.GetCurrent: TDataSet; begin Result := FDataSet; end; function TDataSetEnumerator.MoveNext: boolean; begin if FDoFirst then begin FDoFirst := False; FDataSet.First; end else FDataSet.Next; Result := not FDataSet.EOF; end; { TDataSetHelper } function TDataSetHelper.GetEnumerator: TDataSetEnumerator; begin Result:= TDataSetEnumerator.Create(Self); end; end.
Example:
unit uMain; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf, Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client, DataSet.Enumerator; type TForm1 = class(TForm) memTable1: TFDMemTable; fieldName: TStringField; fieldEmail: TStringField; procedure FormCreate(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); var t: TDataSet; begin memTable1.Open; memTable1.Append; memTable1.FieldByName('Name').AsString := 'Test'; memTable1.FieldByName('Email').AsString := 'test@test.com'; memTable1.Post; memTable1.Append; memTable1.FieldByName('Name').AsString := 'Test 2'; memTable1.FieldByName('Email').AsString := 'test 2@test.com'; memTable1.Post; memTable1.Append; memTable1.FieldByName('Name').AsString := 'Test 3'; memTable1.FieldByName('Email').AsString := 'test 3@test.com'; memTable1.Post; for t in memTable1 do ShowMessage(t.FieldByName('Name').AsString + ' ' + t.FieldByName('Email').AsString); end; end.
416300cookie-checkDelphi enumerate over TDataSet