Fetching details rows with projection

I have this simple invoice / invoice product class
I wan't to retrieve all invoice rows which invoice id is for example in given set.
Since in real data I have many fields and I need just few of them I'm trying to use select
fetch bellow

  llist := fom.Find<TTestInvoiceProducts>.where(Linq['Invoice.Id']<10)
    .Select(TProjections.ProjectionList
    .add(Linq['Invoice.Id'])
    .add(Linq['Product'])).List;
  assert.AreEqual(8,llist.Count);
  assert.IsNotNull(llist[0].invoice);

this will produce sql like . No invoice Id there. If I remove select I get Invoice id and all other fields.

SELECT A.ID AS A_ID, A.PRODUCT AS A_PRODUCT
FROM TEST_INVOICE_PRODUCTS A
  LEFT JOIN TEST_INVOICE B ON (B.ID = A.FInvoice)
WHERE  B.ID < :p_0

classes

  TTestInvoice=class;
  [entity, automapping]
  [id('FId', TIdGenerator.IdentityOrSequence)]
  [Model('Tested')]
  TTestInvoiceProducts = class
  private
    FId:integer;
    FProduct:nullable<string>;
    [ Association( [ { TAssociationProp.Lazy, } TAssociationProp.Required ], [ ] ) ]
    [ JoinColumn( 'FInvoice', [ TColumnProp.Required ] ) ]
    FInvoice: TTestInvoice;
    FCreated: Tdatetime;
    FAmount: double;
    function Getinvoice: TTestInvoice;
    procedure Setinvoice( const Value: TTestInvoice );
  public
    constructor Create(aInvoice: TTestInvoice; aProd: string; aAmount: Double);
    property Amount: double read FAmount write FAmount;
    property Id: integer read FId write Fid;
    property Product:nullable<string> read FProduct Write FProduct;
    property Created: Tdatetime read FCreated Write FCreated;
    property invoice: TTestInvoice  read Getinvoice    write Setinvoice;
  end;
  [entity, automapping]
  [id('FId', TIdGenerator.IdentityOrSequence)]
  [Model('Tested')]
  TTestInvoice=class
  private
    FId:integer;
    FCustomer:nullable<integer>;
    [ ManyValuedAssociation( [ TAssociationProp.Lazy ], CascadeTypeAll, 'FId' ) ]
    [ ForeignJoinColumn( 'FInvoice', [ TColumnProp.Required ] ) ]
    FProducts: proxy< tlist< TTestInvoiceProducts > >; // tuoterivit
    function GetProducts: tlist< TTestInvoiceProducts >;
  public
    constructor Create(aCustomer: Integer);
    destructor Destroy; override;
    Property Id:integer read Fid write FId;
    property Customer:nullable<integer> read FCustomer write FCustomer;
    property Products:  tlist< TTestInvoiceProducts> read getProducts;
  end;

Full DunitX test proj is here https://www.dropbox.com/scl/fi/i3yf8dhyancqf42qoymy4/simpletest.7z?rlkey=3tsa3xxxgwowpuw73l8pv4l00&dl=0

Try this:

1 Like

Thanks, that worked nicely.

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