Entity Selection to Array?

Is there currently any command to take an ORDA object, and efficiently populate traditional arrays from it?

Like this:
<code 4D>
$es1:=ds.Person.all()
$Coll:=New collection(New object(“propertyPath”;“FirstName”);New object(“propertyPath”;“LastName”))
$es2:=$es1.orderBy($Coll)
SELECTION TO ARRAY($es2.FirstName;$aFirst;$es2.Father.FirstName;$aFatherName)

</code 4D>

Or do I need to do a For Each?

I don’t think so but to make it quick : entity to collection then collection to array

There are a number of ways to do this, but specifically without resorting a classic selection, the most efficient way would be with a https://doc.4d.com/4Dv17/4D/17.2/entitySelectiontoCollection.305-4387628.en.htmltoCollection>. The first parameter permits you to include the properties to extract from the entity selection as a comma separated list.

<code 4D>
ARRAY TEXT($atFirstName;0)
ARRAY TEXT($atFatherFirstName;0)
C_COLLECTION($cPersonNames)

$cPersonNames:=ds.Person.all().orderBy(“FirstName, LastName”).toCollection(“FirstName, Father.FirstName”)

COLLECTION TO ARRAY($cPersonNames;
$atFirstName;“FirstName”;
$atFatherFirstName;“Father.FirstName”)
</code 4D>

There are further ways to improve the efficiency of your code. Sorting an entity selection is extremely slow compared with sorting a collection of the same. If the only two values you’re returning are FirstName and Father.FirstName I don’t really see what sense it makes to sort by FirstName then by LastName as in your example, since LastName isn’t included in the result. It would then be possible to orderBy(“FirstName”) on the collection rather than on the entity selection.

Thank you both Dan, and Bertrand!

I don’t really see what sense it makes to sort by FirstName then by LastName as in your example
That was just conceptual code: not a real-life code: I’ll use the concept the you showed in real-life applications.

Hi Tony,

Note you can avoid using the toCollection() member method with something like this:

C_COLLECTION($firstnames;$lastnames)
ARRAY TEXT(_firstnames;0)
ARRAY TEXT(_lastnames;0)

$firstnames:=ds.Persons.all().firstname
$lastnames:=ds.Persons.all().lastname

COLLECTION TO ARRAY($firstnames;_firstnames)
COLLECTION TO ARRAY($lastnames;_lastnames)

: Marie-Sophie LANDRIEU

Note you can avoid using the toCollection() member method
When I see “avoid”, the following question comes: some reason to avoid it? (performance?)

Marie,

Thank you. This method seems very straight-forward: but less efficient, since it makes two separate requests when converting the collection to arrays: one for each field.

It’s still good to know about.

Dan,
Just a followup on your recommendation:
It doesn’t work:
The result of ds.dataClass.all() is an Object: not a collection.

COLLECTION TO ARRAY expects a collection, not an object (nor an entity selection object).

Are you forgetting the .toCollection call at the end? Make sure to specify which fields you want in the resulting collection as well.