4D Blog

Home Product ORDA: Thoroughly compare two entity selections

ORDA: Thoroughly compare two entity selections

October 8, 2021

Product

Since ORDA allows you to work with several entity selections simultaneously, we’re sure you take advantage of this by passing entity selections as a parameter to project methods or to functions of ORDA Data model classes.

To work efficiently with object-oriented programming using as few lines of code as possible, you need robust and optimized functions. That’s why with 4D v19 R3, we are shipping a new function available on the entitySelection object: the selected() function.

Thanks to it, you’ll be able to inspect and compare two entity selections. Let’s see the details!

the new selected() function

Given two entity selections entitySel1 and entitySel2, this function returns the indices of entities of entitySel2 inside entitySel1.

Got it? Here’s an example:

var $invoices; $selection : cs.InvoicesSelection
var $result : Object

$invoices:=ds.Invoices.all()
$selection:=ds.Invoices.query("payment = :1"; "Cash")
$result:=$invoices.selected($selection)
// $result = {ranges:[{start:1,end:1},{start:9,end:12},{start:18,end:18},{start:21,end:25}]}

The selected() function returns an object with a ranges property which is a collection of objects.

Those objects designate a range of entities (start # end) or a single entity (start = end).

The Invoices entities having payment = “Cash” among all invoices are those with indices:

  • 1
  • 9 to 12
  • 18
  • 21 to 25

a concrete use case

Imagine a library with books about different subjects (History, Art, Science, …). Here is the Books table:

On an HTML page, we want to highlight the books about a given subject.

For that, we have a highlight project method called as a 4DACTION with a subject parameter.

highlight project method code:

var $index : Integer
var $subject; $text : Text
var $onSubject : cs.BooksSelection
var $indices; $range : Object
var $titles : Collection
var $i : Integer

ARRAY TEXT($anames; 0)
ARRAY TEXT($avalues; 0

WEB GET VARIABLES($anames; $avalues)

//Get the subject
$index:=Find in array($anames; "subject")
$subject:=$avalues{$index}

//Search books about the given $subject
$onSubject:=ds.Books.query("subject = :1"; $subject)

//Get ranges of books about the subject
$indices:=ds.Books.all().selected($onSubject)

//Build a collection with all the titles
$titles:=ds.Books.all().title

// Loop on the $indices.ranges collection of objects

// $indices.ranges is [{start:0,end:0},{start:2,end:4},{start:7,end:9},{start:11,end:11}]
For each ($range; $indices.ranges)
 For ($i; $range.start; $range.end)
  $titles[$i]:="<b>"+$titles[$i]+"</b>"
 End for
End for each
// Build a text with carriage returns
$text:=$titles.join("<br/>")

WEB SEND TEXT($text)

 

Now let’s run 4DACTION/highlight?subject=History in a browser.

Here is the result:

blank

Now let’s write optimized code with entity selections!

Discuss

Tags ORDA, Programming, v19 R3, v20

Latest related posts

  • December 3, 2025

    Give AI to a 30 years old 4D application

  • November 28, 2025

    ORDA – Handle an event-driven logic during database operations

  • November 27, 2025

    ORDA – Permissions – Restrict / allow web access to the resources in one click

Avatar
Marie-Sophie Landrieu-Yvert
- Product Owner - Marie-Sophie Landrieu-Yvert joined the 4D Product team as a Product Owner in 2017. In this role, she is responsible for writing user stories and translating them into functional specifications. She also ensures that the delivered feature implementation meets the customer's needs. Marie-Sophie graduated from the engineering school ESIGELEC and began her career as an engineer at IBM in 1995. She took part in various projects (maintenance and development projects) and worked as a COBOL developer. She then moved on to work as a UML designer and Java developer. More recently, her main responsibilities included analyzing and writing functional requirements, and coordinating business and development teams.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • AI
  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 Administration AI Artificial Intelligence Build application Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 Administration AI Artificial Intelligence Build application Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor
Subscribe to 4D Newsletter

© 2025 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy