InlineCount for Web-App with RawInvoke (Server side)

Hello Wagner,

I have a search function that I call in the web client using RawInvoke. I pass the values for Skip and Take in my parameter. The server performs the following function:

function KundeSearch_V1( APageSize, APageIndex: Integer; const ASuchtext: String; ASuchmodus: String; AOrderBy: String): TList<TKunde>;
var
  Criteria: TCriteria<TKunde>;
  OrderList: TStringlist;
  i: Integer;
  OrderField: String;
  idxField: Integer;
begin
  Criteria := TXDataOperationContext.Current.GetManager.Find<TKunde>;
  Criteria.CreateAlias( DicKunde.Kunde.Person.AssociationName, 'p');
  makeFilter_V1( Criteria, 'p.', ASuchtext, ASuchmodus);  // complex Filter !!!

  if APageSize > 0 then
    Criteria.Skip( APageIndex*APageSize).Take( APageSize);
  Result := Criteria.List;
end;

I would now like to determine the "inlinecount" in the server at the same time.
Is there a function or parameter I can use here?
Or can I use the "TCriteria" to issue a count command myself?

Best regards
Thomas

e.g.

TheCount := Criteria.Count;
or
TheCount := GetCount( Criteria);

before executing .List;

searching ... and found something

Is this solution right?

var
  Criteria: TCriteria<TKunde>;
  CriteriaCount: TCriteria;
  CountResult: TCriteriaResult;
  SearchCount: Int64;
begin
  (...)

  CriteriaCount := Criteria.Clone;
  try
    CriteriaCount.SetProjections( TProjections.Sql<Int64>('Count(*)'));
    CountResult := CriteriaCount.UniqueValue;
    SearchCount := CountResult.Values[0];
    DoLog( 'SearchCount='+SearchCount.ToString);
  finally
    CountResult.Free;
    //CriteriaCount.Free;  // Exception if Free
  end;

I think, this is the solution.
Here a function to copy.
If Wagner confirms this. :slight_smile:

class function TDBTools.GetCriteriaCount( ACriteria: TCriteria): Int64;
var
  CriteriaCount: TCriteria;
  CountResult: TCriteriaResult;
begin
  CriteriaCount := ACriteria.Clone;
  try
    CriteriaCount.SetProjections( TProjections.Sql<Int64>('Count(*)'));
    CountResult := CriteriaCount.UniqueValue;
    Result := CountResult.Values[0];
  finally
    CountResult.Free;
    //CriteriaCount.Free;
  end;
end;

1 Like

That will work fine.
You can add more code just in case your Criteria already has the paging or have order (some databases don't allow ORDER BY clause when you do a Count(*). So you can also add this:

        CriteriaCount.Take(-1);
        CriteriaCount.Skip(0);
        CriteriaCount.OrderItems.Clear;

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.