TCriteria - Test if assigned

I am struggling with TCriteria and appreciate every support! Thanks.

This Code spilts a word in pieces and creates a search in BEZEICHNUNG and SYNONYM. If the piece is shorter than 3 Chars, the search is in LEBENSMITTELCD.

However, if there is only one word and Shorter then 3 Chars, the second condition is never met and ExpressionA and ExpressionB are undefined.


  bFirstGroup := true;
  for s in sSearch.Split([' ']) do begin
    if s.Length <= 2 then begin
      if bFirstGroup then begin
        ExpressionGroup := Linq.like('LebensmittelCD', s + '%');
        bFirstGroup := false;
      end else begin
        ExpressionGroup := ExpressionGroup or Linq.like('LebensmittelCD', s + '%');
      end;
    end;
  end;
  bFirst := true;
  for s in sSearch.Split([' ']) do begin
    if s.Length > 2 then begin
      if bFirst then begin
        ExpressionA := Linq.like('Bezeichnung', '%' + s + '%');
        ExpressionB := Linq.like('Synonym', '%' + s + '%');
        bFirst := false;
      end else begin
        ExpressionA := ExpressionA and Linq.like('Bezeichnung', '%' + s + '%');
        ExpressionB := ExpressionB and Linq.like('Synonym', '%' + s + '%');
      end;
    end;
  end;
  if bFirstGroup then begin
    Criteria.Add(ExpressionA or ExpressionB);
  end else begin
    Criteria.Add(ExpressionGroup and (ExpressionA or ExpressionB)); < ---- works, although A and B are undefined
  end;
  DataBLS := Criteria.List; < ------------------- Exception is raised


Is there any way to check, if a TLinqExpression holds an expression? Introducing a flag for every Expression to trace if it?s initialized seems not very elegant.

What do you recommend?

You could initialize the expressions to an innocuous expression:




var
  Expr: TLinqExpression;
  DefaultOrExpr: TLinqExpression;
  DefaultAndExpr: TLinqExpression;
  HasExpressionGroup: Boolean;
begin
  DefaultOrExpr := (Linq.Literal<Integer>(0) <> Linq.Literal<Integer>(0));
  DefaultAndExpr := (Linq.Literal<Integer>(0) = Linq.Literal<Integer>(0));


  ExpressionGroup := DefaultOrExpr;
  HasExpressionGroup := Boolean;
  for s in sSearch.Split([' ']) do
    if s.Length <= 2 then
    begin
      ExpressionGroup := ExpressionGroup or Linq['LebensmittelCD'].StartsWith(s);
      HasExpressionGroup := True;
    end;


  ExpressionA := DefaultAndExpr;
  ExpressionB := DefaultAndExpr;
  for s in sSearch.Split([' ']) do
    if s.Length > 2 then begin
      ExpressionA := ExpressionA and Linq.like('Bezeichnung', '%' + s + '%');
      ExpressionB := ExpressionB and Linq.like('Synonym', '%' + s + '%');
    end;


  if HasExpressionGroup then begin
    Criteria.Add(ExpressionA or ExpressionB);
  end else begin
    Criteria.Add(ExpressionGroup and (ExpressionA or ExpressionB));
  end;

I see. Thank you.