LinqQuerySample.lpr 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. program LinqQuerySample;
  2. {$mode delphi}
  3. uses
  4. SysUtils,
  5. Quick.Commons,
  6. Quick.Console,
  7. Quick.Chrono,
  8. Quick.Arrays,
  9. Quick.Linq,
  10. Quick.Expression;
  11. const
  12. numusers = 0;
  13. UserNames : array[0..25] of string = ('Cliff','Alan','Anna','Phil','John','Michel','Jennifer','Peter','Brandon','Joe','Steve','Lorraine','Bill','Tom','Norma','Martin','Steffan','Wilma','Derek','Lewis','Paul',
  14. 'Erik','Robert','Nicolas','Frederik','Rose');
  15. UserSurnames : array[0..25] of string = ('Gordon','Summer','Huan','Paterson','Johnson','Michelson','Smith','Peterson','Miller','McCarney','Roller','Gonzalez','Thomson','Muller','Jefferson','Volkov','Matheu','Morrison','Newman','Lover','Sunday',
  16. 'Roberts','Landon','Yuri','Paris','Levis');
  17. type
  18. TLoginInfo = class
  19. private
  20. fUserName : string;
  21. fUserPassword : string;
  22. fLocked : Boolean;
  23. published
  24. property UserName : string read fUserName write fUserName;
  25. property UserPassword : string read fUserPassword write fUserPassword;
  26. property Locked : Boolean read fLocked write fLocked;
  27. end;
  28. { TUser }
  29. TUser = class
  30. private
  31. fId : Int64;
  32. fName : string;
  33. fSurName : string;
  34. fAge : Integer;
  35. fLoginInfo : TLoginInfo;
  36. public
  37. constructor Create;
  38. destructor Destroy; override;
  39. published
  40. property Id : Int64 read fId write fId;
  41. property Name : string read fName write fName;
  42. property SurName : string read fSurName write fSurName;
  43. property Age : Integer read fAge write fAge;
  44. property LoginInfo : TLoginInfo read fLoginInfo write fLoginInfo;
  45. end;
  46. var
  47. users : TXArray<TUser>;
  48. user : TUser;
  49. i : Integer;
  50. n : Integer;
  51. crono : TChronometer;
  52. { TUser }
  53. constructor TUser.Create;
  54. begin
  55. fLoginInfo := TLoginInfo.Create;
  56. end;
  57. destructor TUser.Destroy;
  58. begin
  59. fLoginInfo.Free;
  60. inherited Destroy;
  61. end;
  62. begin
  63. try
  64. cout('Generating list...',etInfo);
  65. //generate first dummy entries
  66. for i := 1 to numusers - high(UserNames) do
  67. begin
  68. user := TUser.Create;
  69. user.Id := Random(999999999999999);
  70. user.Name := 'Name' + i.ToString;
  71. user.SurName := 'SurName' + i.ToString;
  72. user.Age := 18 + Random(20);
  73. users.Add(user);
  74. end;
  75. //generate real entries to search
  76. for i := 0 to high(UserNames) do
  77. begin
  78. user := TUser.Create;
  79. user.Id := Random(999999999999999);
  80. user.Name := UserNames[i];
  81. user.SurName := UserSurnames[i];
  82. user.Age := 18 + Random(20);
  83. user.LoginInfo.Username := UserNames[i];
  84. user.LoginInfo.UserPassword := RandomPassword(8,[pfIncludeNumbers,pfIncludeSigns]);
  85. user.LoginInfo.Locked := False;
  86. users.Add(user);
  87. end;
  88. crono := TChronometer.Create;
  89. //test search by normal iteration
  90. user := nil;
  91. crono.Start;
  92. for i := 0 to users.Count - 1 do
  93. begin
  94. //if (users[i].Name = 'Anus') or (users[i].SurName = 'Smith') then
  95. if users[i].Name = 'Peter' then
  96. begin
  97. crono.Stop;
  98. user := users[i];
  99. Break;
  100. end;
  101. end;
  102. if user <> nil then cout('Found by Iteration: %s %s at %d position in %s',[user.Name,user.SurName,i,crono.ElapsedTime],etSuccess)
  103. else cout('Not found by Iteration!',etError);
  104. //test search by Linq iteration
  105. crono.Start;
  106. user := TLinq<TUser>.From(users).Where('(Name = ?) OR (SurName = ?)',['Anus','Smith']).OrderBy('Name').SelectFirst;
  107. //user := TLinq<TUser>.From(users).Where('LoginInfo.UserName = ?',['Cliff']).SelectFirst;
  108. crono.Stop;
  109. if user <> nil then cout('Found by Linq: %s %s in %s',[user.Name,user.SurName,crono.ElapsedTime],etSuccess)
  110. else cout('Not found by Linq! (%s)',[crono.ElapsedTime],etError);
  111. //ConsoleWaitForEnterKey;
  112. cout('Multi results:',etInfo);
  113. //test search by normal iteration
  114. user := nil;
  115. n := 0;
  116. cout('Found by Iteration:',etInfo);
  117. for i := 0 to users.Count - 1 do
  118. begin
  119. //if ((users[i].Name = 'Anna') or (users[i].Age > 30)) or (users[i].SurName = 'Smith') then
  120. //if users[i].Age > 18 then
  121. if (users[i].Name = 'Anna') then
  122. begin
  123. Inc(n);
  124. user := users[i];
  125. cout('%d. %s %s',[n,user.Name,user.SurName],etSuccess)
  126. end;
  127. end;
  128. if user = nil then cout('Not found by Iteration!',etError);
  129. //test search by Linq iteration
  130. user := nil;
  131. n := 0;
  132. cout('Found by Linq:',etInfo);
  133. //TLinq.From<TUser>(users).Where('SurName Like ?',['p%']).Delete;
  134. TLinq<TUser>.From(users).Where('Name = ?',['Peter']).Update(['Name'],['Poter']);
  135. //for user in TLinq<TUser>.From(users).Where('(Name = ?) OR (SurName = ?) OR (SurName = ?)',['Peter','Smith','Huan']).Select do
  136. //for user in TLinq<TUser>.From(users).Where('(Name = ?) OR (Age > ?) OR (SurName = ?)',['Anna',30,'Smith']).Select do
  137. //for user in TLinq<TUser>.Fromusers).Where('Age > ?',[18]).Select do
  138. //for user in TLinq<TUser>.From(users).Where('SurName Like ?',['%son']).Select do
  139. //for user in TLinq<TUser>.From(users).Where('SurName Like ?',['p%']).Select do
  140. //for user in TLinq<TUser>.From(users).Where('1 = 1',[]).Select do
  141. for user in TLinq<TUser>.From(users).Where('(LoginInfo.UserName Like ?) OR (LoginInfo.UserName Like ?)',['p%','a%'])
  142. .OrderBy('Name')
  143. .Select do
  144. begin
  145. Inc(n);
  146. cout('%d. %s %s',[n,user.Name,user.SurName],etSuccess);
  147. end;
  148. if user = nil then cout('Not found by Linq!',etError);
  149. cout('Press a key to Exit',etInfo);
  150. Readln;
  151. crono.Free;
  152. except
  153. on E: Exception do
  154. cout('%s : %s',[E.ClassName,E.Message],etError);
  155. end;
  156. end.