Complex sorting results with ManyValuedAssociation

Complex sorting results with ManyValuedAssociation

Good afternoon folks. Someone can help me with this doubt?
How can I order my results using "Sequence" Field of my classes TExpression and TCondition ?

I'd like of bring TRule class and order First Expressios by Sequence and Conditions by Sequence.

Something like this:

Rule := Manager.Find<TRule>.CreateAlias('Expressions??????','Expressions')
                           .CreateAlias('Expressions??????.Conditions','Conditions')
                           .Add(TLinq.Eq('id',id)).AddOrder(TOrder.Asc(Linq['Conditions.Sequence'])).UniqueResult;
                          
My classes are below:

  [Entity]
  [Automapping]
  TRule = class
  private
    FId: Int64;
    FDescription: String;
    FPriority: Integer;
    [ManyValuedAssociation([], CascadeTypeAllRemoveOrphan)]
    FExpressions: TList<TExpression>;
    [ManyValuedAssociation([], CascadeTypeAllRemoveOrphan)]
    FActions: TList<TAction>;
  public
    property Id: Int64 read FId write FId;
    property Description: String read FDescription write FDescription;
    property Priority: Integer read FPrioridade write FPrioridade;
    property Expressions: TList<TExpression> read FExpression write FExpression;
    property Actions: TList<TAction> read FActions write FActions;
  end;
 
  [Entity]
  [Automapping]
  TExpression = class
  private
    FId: Int64;
    FSequence: Integer;
    FOperator: TOperator;
    [ManyValuedAssociation([], CascadeTypeAllRemoveOrphan)]
    FConditions: TList<Condition>;
  public
    property Id: Int64 read FId write FId;
    property Sequence: Integer read FSequence write FSequence;
    property Conditions: TList<TCondition> read FConditions write FConditions;
    property Operator: TOperator read FOperator write FOperator;
  end;

  [Entity]
  [Automapping]
  TCondition = class
  private
    FId: Int64;
    FSequence: Integer;
    FOperator: TOperator;
    FAttribute: TAttribute;
    FValue: string;
  public
    property Id: Int64 read FId write FId;
    property Sequence: Integer read FSequence write FSequence;
    property Operator: TOperator read FOperator write FOperator;
    property Attribute: TAttribute read FAttribute write FAttribute;
    property Value: string read FValue write FValue;
  end;
 

There is no way to specify order of associated lists using the criteria.

It should be hard coded by using the [OrderBy] attribute:

http://www.tmssoftware.biz/business/aurelius/doc/web/orderbyattribute.html

Or you should bring the list using a separate criteria, but in this case you should have a bidirectional association so you can filter out expressions and conditions by a specific rule id.
Thank You Wagner, the OrderBy attribute solve my problem.



Would be amazing if Aurelius could do that! rss

If I get correctly the business logic behind your entities, you've got expressions and conditions that can are added sequentially to a rule and, thus, you want to retrieve them in the order they were created in the database. 


This is a flawed design. If you base the ordering of the conditions and expressions on the ID the database assigns you open the possibility for situations that can be hard to remedy. For example, how will you manage the situation that the order of the expressions can be changed? Or, if you migrate to a database that uses GUIDs as IDs?

The simplest solution is to create another field (e.g. Rank) and keep the order there

Also, are you meant to write 'Expressions.ID'? The way you've got this now means 'Rule.ID'