ScalarQueryExpression with join

Posts   
 
    
Posts: 254
Joined: 16-Nov-2006
# Posted on: 02-Apr-2007 15:58:31   

Guys,

I'm actually writing similar code to the HnD forum however this is very limited, just to attach files to documents e.g. orders / invoices.

I want to achieve something similar to HnD when creating a bindable field which indicates how many attachments are associated with a document. The HnD source has code such as this

 // Add a scalar query expression to the list of fields in the typedlist. The scalar query expression 
            // performs a SELECT COUNT(AttachmentID) FROM Attachments WHERE MessageID = Message.MessageID
            // query. 
            fields.DefineField(new EntityField("AmountOfAttachments",
                                new ScalarQueryExpression(AttachmentFields.AttachmentID.SetAggregateFunction(AggregateFunction.Count),
                                    (AttachmentFields.MessageID == MessageFields.MessageID))), index);

However rather than having a foreign key to the message table directly in the attachment table I have a link table which joins the conversation message and the attachment record directly ( this allows other tables e.g. one which links purchase orders to attachments ). We have two core tables here

Attachment - AttachmentId and the blob ConversationAttachment - AttachmentId and ConversationId

How should I change this code to incorporate this additional join, or should I not use the ScalarQueryExpression and instead create a relation collection.

Thoughts?

Aurelien avatar
Aurelien
Support Team
Posts: 162
Joined: 28-Jun-2006
# Posted on: 02-Apr-2007 16:20:04   

Hi,

Maybe you can use the ScalarQueryExpression constructor witch take a relation collection as parameter ?

Posts: 254
Joined: 16-Nov-2006
# Posted on: 02-Apr-2007 16:27:34   

Ignore that sorry I can get around this by simply querying directly on the link table ConversationAttachment and performing an aggregate count on the records matching attachmentid directly.