Hello,
how can I fast get the last ID (not count) from an Table with XDataClient ?
At the moment I use following code, but the table has about 70000 entries and so it was very slowly to became the last id.
AktuelleRaumID := 0;
lRaumdatenList := LXDataClient.List<TTBL_RAUMDATEN>('?$ORDERBY=ID_RAUMDATEN'); lRaumdaten := lRaumdatenList.Last;
AktuelleRaumID := lRaumdaten.ID_RAUMDATEN;
Thanks for ur answer.
Hello Jens,
List is not the answer for everything, unfortunately. You should create a service to return it. You can simply do a simple one that returns last id for that specific class, or you could use a more generic one like this:
unit LastIdService;
interface
uses
XData.Service.Common;
type
[ServiceContract]
ILastIdService = interface(IInvokable)
['{E3A551E5-950C-4DEF-8FED-3E514A4178EE}']
[HttpGet] function GetLastId(const ClassName: string): Integer;
end;
[ServiceImplementation]
TLastIdService = class(TInterfacedObject, ILastIdService)
private
function GetLastId(const ClassName: string): Integer;
end;
implementation
uses
System.Classes, Aurelius.Criteria.Base,
XData.Server.Module, Aurelius.Criteria.Linq;
{ TLastIdService }
function TLastIdService.GetLastId(const ClassName: string): Integer;
var
Value: TCriteriaResult;
IdMemberName: string;
Clazz: TClass;
begin
Clazz := TXDataOperationContext.Current.GetManager.Explorer
.Hierarchy.FindClassByName(ClassName);
IdMemberName := TXDataOperationContext.Current.GetManager.Explorer
.GetIdColumns(Clazz)[0].Optimization.MemberName;
Value := TXDataOperationContext.Current.GetManager
.CreateCriteria(Clazz)
.Select(Linq[IdMemberName].Max)
.UniqueValue;
try
Result := Value[0];
finally
Value.Free;
end;
end;
initialization
RegisterServiceType(TLastIdService);
RegisterServiceType(TypeInfo(ILastIdService));
end.
Then from client code you can call it like this:
AktuelleRaumID := LXDataClient.Service<ILastIdService>.GetLastId('TTBL_RAUMDATEN');
Note that this solution only works for entities with a single property id (not composite) and of type integer.
Wagner R. Landgraf2018-05-31 15:40:16