Quick Objects Documentation Akal Tech Logo
Adding Calculated Columns To Results

Glossary Item Box

Business Logic Framework - How To:

Assuming that you would like to display a list of customers and you would like to create a new column that shows the complete name of the customer. This can be done in two separate ways depending on your requirements. The calculated column can be created at database level or it can be created in the result set returned by the database. The following code snippet shows you how to add a calculated column at the database level:

 

1 using (Customers customer = new Customers())  
2 {  
3  
4     // Create a new Field of Type StringField  
5     // Passing the customer object instance to the field constructor will simplify the process and this field will automatically be included in the result set.  
6     StringField fullName = new StringField(customer);  
7  
8  
9  
10     // We will mark the IsCustom property to true for this field to let the business object know that this field is a custom field and should not be treated like a regular field.  
11     fullName.IsCustom = true;  
12  
13     // Since there is no column in the underlying table named "FullName", we want to make sure that we don't include this field in Insert or Updates  
14     // The following line shows how to exclude this field from Inserts or Updates.  
15     fullName.AllowSave = false;  
16  
17     // We must assign an alias (ResultSetName) to the fullName field object. The column in the result set will be named with this value.  
18     // NOTE: We recommend that you use the GetResultSetName() method of the field when trying to access the column from the result set as this will automatically make sure you get the correct column name.  
19     fullName.ResultSetName = "FullName";  
20  
21  
22     // In a Custom field the Name property is used to specify any calculation expression possible.  
23     // Here we specify that first the LastName column value should appear then it should be appended by  
24     // a comma and a space. Lastly the FirstName value will be appended.  
25     fullName.Name = customer.LastName.Name.Value + " + ', ' + " + customer.FirstName.Name.Value;  
26  
27     this.GridView.DataSource = customer.Find();  
28     this.GridView.DataMember = customer.GetResultSetName();  
29  
30 }  
31  
1 Using customer As New Customers()   
2       
3     ' Create a new Field of Type StringField   
4     ' Passing the customer object instance to the field constructor will simplify the process and this field will automatically be included in the result set.   
5     Dim fullName As New StringField(customer)   
6       
7       
8       
9     ' We will mark the IsCustom property to true for this field to let the business object know that this field is a custom field and should not be treated like a regular field.   
10     fullName.IsCustom = True   
11       
12     ' Since there is no column in the underlying table named "FullName", we want to make sure that we don't include this field in Insert or Updates   
13     ' The following line shows how to exclude this field from Inserts or Updates.   
14     fullName.AllowSave = False   
15       
16     ' We must assign an alias (ResultSetName) to the fullName field object. The column in the result set will be named with this value.   
17     ' NOTE: We recommend that you use the GetResultSetName() method of the field when trying to access the column from the result set as this will automatically make sure you get the correct column name.   
18     fullName.ResultSetName = "FullName"   
19       
20       
21     ' In a Custom field the Name property is used to specify any calculation expression possible.   
22     ' Here we specify that first the LastName column value should appear then it should be appended by   
23     ' a comma and a space. Lastly the FirstName value will be appended.   
24     fullName.Name = customer.LastName.Name.Value + " + ', ' + " + customer.FirstName.Name.Value   
25       
26     Me.GridView.DataSource = customer.Find()   
27           
28     Me.GridView.DataMember = customer.GetResultSetName()   
29 End Using  


The other alternative is to add a calculated column to the DataSet returned by Find method. Here is an example:

 

1 using (Customers customer = new Customers())  
2 {  
3  
4     // Run the Find method, which will return all rows available in Customers table since there is no search criteria specified.  
5     customer.Find();  
6  
7     // Now check and make sure that the Find was successful and there is a table containing the customer data in the DataSet returned by Find.  
8     if (customer.ResultSet.Tables.Contains(customer.GetResultSetName()))  
9     {  
10  
11         // We can use the DataSet's calculated column capability to add a calculated column.  
12         DataColumn dc = new DataColumn();  
13         dc.ColumnName = "FullName";  
14         dc.Expression = "[LastName] + ', ' + [FirstName]";  
15  
16         // the following line will add this calculated column to the table containing the customer data.  
17         customer.ResultSet.Tables[customer.GetResultSetName()].Columns.Add(dc);  
18  
19         // We can access the DataSet returned by the Find method in the "ResultSet" property of the business object.  
20         // NOTE: By default the "AddResultsToDataSet" is set to true and hence the data will be available in the ResultSet property. However, if you set the  
21         // "AddResultsToDataSet" to false, then the DataSet will only be returned by the Find method and will not be stored in the "ResultSet" property.  
22         this.GridView.DataSource = customer.ResultSet;  
23         this.GridView.DataMember = customer.GetResultSetName();  
24  
25     }  
26  
27 }  
28  
1 Using customer As New Customers()   
2       
3     ' Run the Find method, which will return all rows available in Customers table since there is no search criteria specified.   
4     customer.Find()   
5       
6     ' Now check and make sure that the Find was successful and there is a table containing the customer data in the DataSet returned by Find.   
7     If customer.ResultSet.Tables.Contains(customer.GetResultSetName()) Then   
8           
9         ' We can use the DataSet's calculated column capability to add a calculated column.   
10         Dim dc As New DataColumn()   
11         dc.ColumnName = "FullName"   
12         dc.Expression = "[LastName] + ', ' + [FirstName]"   
13           
14         ' the following line will add this calculated column to the table containing the customer data.   
15         customer.ResultSet.Tables(customer.GetResultSetName()).Columns.Add(dc)   
16           
17         ' We can access the DataSet returned by the Find method in the "ResultSet" property of the business object.   
18         ' NOTE: By default the "AddResultsToDataSet" is set to true and hence the data will be available in the ResultSet property. However, if you set the   
19         ' "AddResultsToDataSet" to false, then the DataSet will only be returned by the Find method and will not be stored in the "ResultSet" property.   
20         Me.GridView.DataSource = customer.ResultSet   
21               
22         Me.GridView.DataMember = customer.GetResultSetName()   
23           
24     End If   
25 End Using  


Comparison of the two methods:

Database Level DataSet Level
Performed at the SQL Level by the Database Performed by the DataSet in the memory
The expression code you write is database dependent. Hence if you choose to move your application to a different database server you will need to make modifications to your custom column for it to work correctly. The expression code is NOT database dependent. Hence the code you write will work irrespective of the underlying database type.
Unable to calculate values while in disconnected state and hence the values only are updated after insert/update is completed to the database and database is queried for the same data again. Values are automatically updated in the disconnected state. There is no need to go back and query the database again to update calculated columns if any of the underlying values have changed.

This above comparison is meant to be brief highlight of each method and is no way meant as a comprehensive analysis of the performance of each of these two techniques.