Business Logic Framework - How To:
It is often that a developer needs to add a column/field in the select statement that is computed based on a sub query. Quick Objects Business Logic Framework introduced a new Field object that allows you to add a Aggregate SubQuery with just couple of lines of code. This sample will introduce you to the process.
In this sample we display a list of customers and for each customer we also show a sum of the OrderAmount for all orders placed by that customer.
The new class AggregateSubQueryField provides a very simple way of defining such a criteria. In this example we use an overload of the constructor to set the following properties.
- BusinessObject - This instance should be the instance that will run the select command. The AggregateSubQueryField instance will automatically add itself to the Fields collection of the passed in object instance.
- GroupTypes - Value indicating which aggregate function to use. None and GroupBy are not valid choices.
- AggregateField - This field is used to perform the aggregate function on. For example if you want to perform "Sum" on the OrderAmount, you will pass the instance of OrderAmount field for this parameter.
- ResultSetName - This value will be used as the column name in the results returned by the database. You must set this value to a non-null value and it must be unique.
- LeftField and RightField - Pass the two instances of the two Field objects that the main object and sub query object have in common. For example, in this sample we pass the customer.CustomerID and o.CustomerID. If you need to set multiple, then you can add more pairs in the JoinedFields collection.
| // This example we will show a list of customers, and one extra column will show the Total Amount of Orders by each customer. | |
| using (Customers customer = new Customers()) | |
| { | |
| // Aggregate Sub Query will be for OrderAmount SUM. We create a new instance of Orders. | |
| // Note: We are not creating a Join with Order object as we don't want to include all the information from Orders table. We only want to get the total amount of all orders for each of the customers. | |
| Orders o = new Orders(); | |
| // Create a new instance of AggregateSubQueryField. | |
| AggregateSubQueryField af1 = new AggregateSubQueryField(customer, GroupTypes.Sum, o.OrderAmount, "Order Total", | |
| customer.CustomerID, o.CustomerID); | |
| // We also want to sort by this Aggregate Sub Query column in the Descending order. | |
| af1.Sort = SortTypes.Descending; | |
| customer.Find(); | |
| if (customer.AffectedRecords > 0) | |
| { | |
| this.dataGridView1.DataSource = customer.ResultTable; | |
| } | |
| } | |
; LINE-HEIGHT! important; BORDER-BOTTOM: rgb(127,157,185) 1px solid; FONT-FAMILY: Courier New; BACKGROUND-COLOR: white">
| ' This example we will show a list of customers, and one extra column will show the Total Amount of Orders by each customer. | |
| Using customer As New Customers() | |
| ' Aggregate Sub Query will be for OrderAmount SUM. We create a new instance of Orders. | |
| ' Note: We are not creating a Join with Order object as we don't want to include all the information from Orders table. We only want to get the total amount of all orders for each of the customers. | |
| Dim o As New Orders() | |
| ' Create a new instance of AggregateSubQueryField. | |
| Dim af1 As New AggregateSubQueryField(customer, GroupTypes.Sum, o.OrderAmount, "Order Total", customer.CustomerID, o.CustomerID) | |
| ' We also want to sort by this Aggregate Sub Query column in the Descending order. | |
| af1.Sort = SortTypes.Descending | |
| customer.Find() | |
| If customer.AffectedRecords > 0 Then | |
| Me.dataGridView1.DataSource = customer.ResultTable | |
| End If | |
| End Using | |
