Add a calculated column to the XData result list

Hello Wagner,

I want to add a calculated field (or more) to a table query in my REST server in order to send it with XData.
It doesn't matter whether I later have to work with a standard query (REST Get List standard endpoint) or a service function.

The entity list passed with XData must therefore be expanded to include the field.

I have no idea what to do.

  1. Calculate the field within the query
  2. Send the extended result list


Hello Thomas,

I got confused with the terms. What is "add a calculated field to a table query in my REST Server"?

Are you using an automatic CRUD endpoint or a service operation endpoint?
Are you building a query component manually server-side?
Are you referring to calculate fields in a query component or in an entity class?

Can you please be more specific, I really didn't understand what you want to achieve, thanks.

It is simple,
As an an example: (dummy example)
A table: Name:String / Quantity:Integer / Price:Float
The client application wants to display a table with : Name, Quantity, Price, AND Total:Float (Total = Quantity * Price)
The client application is stupid. The client can only show

The client can use automatic CRUD, but I can also make a service endpoint.

I usually get a TList<TMyEntity>.
But there are only Name,Quantity,Price included.
I'm looking for a quick solution to extend this list with a field/column that is calculated on the server.

And here are the two questions

  1. Where and how to calculate the value (an event in XDataServer or an event in Aurelius)
  2. How to transfer the extended list most easily

Currently I have simply extended the entity with the desired fields (also in the database) and calculate the values in Aurelius with OnInserting/OnUpdating. But I really didn't want to add these fields to the database.

Is there another, simpler solution?

There are a few solutions. First is to add calculated fields to your entity. Just create a Total property, set a getter for it and calculate the total based on the getter. Then make such property available in JSON by adding XDataJsonProperty attribute to it. An example:

Another option is to create your own DTO class (TMyDTOEntity) and return it from a service operation. Then create and fill its properties manually from TMyEntity, and fill in the missing properties accordingly.

Thank you, Wagner. Now I have 3 possibilities.
I can choose the best solution for any case.

1 Like

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