({{, (RefLink[EntityFunction, paclet:ref/EntityFunction])[x,body] 
is a function with a single formal parameter x, to be used in (RefLink[EntityValue, paclet:ref/EntityValue]) and related functions.}, {, (RefLink[EntityFunction, paclet:ref/EntityFunction])[{(Subscript[x, 1]),(Subscript[x, 2]),…},body]
is an (RefLink[EntityFunction, paclet:ref/EntityFunction]) with a list of formal parameters.}})

OpenersNotesSection(Spacer1)Details

(RefLink[EntityFunction, paclet:ref/EntityFunction])[x, body] behaves exactly like an (RefLink[EntityProperty, paclet:ref/EntityProperty]) and can be used directly in (RefLink[EntityValue, paclet:ref/EntityValue]).

In (RefLink[EntityFunction, paclet:ref/EntityFunction])[x,body], x is usually bound to a single (RefLink[Entity, paclet:ref/Entity]) at a time. The second argument of (RefLink[AggregatedEntityClass, paclet:ref/AggregatedEntityClass]) is an exception to this rule, since the formal parameter will bind to each group of entities.

In the context of the second argument of (RefLink[AggregatedEntityClass, paclet:ref/AggregatedEntityClass]) for relational-database-backed entities, the following aggregation functions are supported:

(RefLink[EntityFunction, paclet:ref/EntityFunction])[{left,right},body] can appear as the condition for (RefLink[CombinedEntityClass, paclet:ref/CombinedEntityClass]). Here, left will bind to each (RefLink[Entity, paclet:ref/Entity]) in the first argument of (RefLink[CombinedEntityClass, paclet:ref/CombinedEntityClass]) and right to each (RefLink[Entity, paclet:ref/Entity]) in the second argument.

When dealing with entities registered in a relational-database-backed (RefLink[EntityStore, paclet:ref/EntityStore]), only the following forms can be compiled to SQL:

In the context of relational-database-backed entities, the functions above might have limited functionality with respect to their Wolfram Language counterpart. For example, SQL lacks support for complex numbers; because of this, the domains of numerical functions are limited to regions where they are real valued. Symbolic evaluation is generally not available. Precision-tracking semantics may also vary.

OpenersPrimaryExamplesSection(Spacer1)Examples  (10)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

Compute the ratio between atomic mass and atomic number for all noble gases:

EntityValue[(EntityClass["noble gases", EntityClass["Element","NobleGas"], "EntityClass[\"Element\", \"NobleGas\"]", "elements"]), EntityFunction[e,N[e["AtomicMass"]/e["AtomicNumber"]]]]

(RefLink[EntityFunction, paclet:ref/EntityFunction]) can be applied directly to a single entity, like (RefLink[EntityProperty, paclet:ref/EntityProperty]):

EntityFunction[c, c[]/c[(EntityProperty["population", EntityProperty["Country","Population"], "EntityProperty[\"Country\", \"Population\"]"])]][(Entity["Ireland", Entity["Country","Ireland"], "Entity[\"Country\", \"Ireland\"]", "country"])]

Use (RefLink[EntityFunction, paclet:ref/EntityFunction]) as a condition for (RefLink[FilteredEntityClass, paclet:ref/FilteredEntityClass]):

EntityList[FilteredEntityClass["Country",EntityFunction[c,c["Population"]>(Quantity[(10^8), "people", people, "People"])]]]

OpenersExampleSection2(Spacer1)Properties & Relations  (6)

Use in (RefLink[ExtendedEntityClass, paclet:ref/ExtendedEntityClass]) to add a new computed property:

ExtendedEntityClass[EntityClass["Country", "G8"], "AdultFraction" -> EntityFunction[c,c["AdultPopulation"]/c["Population"]]]["AdultFraction"]

Use in (RefLink[FilteredEntityClass, paclet:ref/FilteredEntityClass]) as a predicate to select certain entities:

EntityList[FilteredEntityClass["Country",EntityFunction[c,c["Population"]>(Quantity[1 (10^8), "people", people, "People"]) || c["Continent"]===(EntityClass["Oceania", EntityClass["Country","Oceania"], "EntityClass[\"Country\", \"Oceania\"]", "countries"])]]]

Use in (RefLink[SortedEntityClass, paclet:ref/SortedEntityClass]) as a sorting criterion:

EntityList[SortedEntityClass[(EntityClass["Europe", EntityClass["Country","Europe"], "EntityClass[\"Country\", \"Europe\"]", "countries"]), EntityFunction[c, c[]/c[(EntityProperty["population", EntityProperty["Country","Population"], "EntityProperty[\"Country\", \"Population\"]"])]-> "Descending"],3]]

Use in the second argument of (RefLink[AggregatedEntityClass, paclet:ref/AggregatedEntityClass]) as an aggregator:

AggregatedEntityClass["Country", "TotalPopulation"->EntityFunction[e,Total[e[(EntityProperty["population", EntityProperty["Country","Population"], "EntityProperty[\"Country\", \"Population\"]"])]]]]["TotalPopulation"]

Use in the third argument of (RefLink[AggregatedEntityClass, paclet:ref/AggregatedEntityClass]) as a grouping criterion:

AggregatedEntityClass["Country", "MeanArea"->EntityFunction[e,Mean[e["Area"]]],"RoundedPopulation"-> EntityFunction[c,Round[c["Population"],100000000]]][{"RoundedPopulation","MeanArea"}]

Use in (RefLink[CombinedEntityClass, paclet:ref/CombinedEntityClass]) as a combining criterion:

CombinedEntityClass["Isotope", (EntityClass["period 1 elements", EntityClass["Element","Period1"], "EntityClass[\"Element\", \"Period1\"]", "elements"]),EntityFunction[{i, e},i["AtomicMass"]< e["AtomicMass"]]][{EntityProperty["Isotope", "Name"],EntityProperty["Element", "Name"]}]

OpenersExampleSection3(Spacer1)Possible Issues  (1)

SQL doesn't support all operations supported by the Wolfram Language:

EntityRegister[EntityStore[RelationalDatabase[FindFile["ExampleData/ecommerce-database.sqlite"]]]]

EntityFunction[e, ToUpperCase[e["firstName"]]][EntityClass["employees", {}]]

Mathematical operations in SQL only support real numbers; performing operations that would return a complex number might result in (RefLink[Missing, paclet:ref/Missing]) or a runtime error, depending on the backend:

EntityFunction[p,Sqrt[- p["amount"]]][EntityClass["payments", {}]]//Short

SQL doesn't support returning anything but scalars from properties or (RefLink[EntityFunction, paclet:ref/EntityFunction]):

EntityClass["customers", {}][EntityFunction[c, c["payments"]["amount"]]]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[EntityProperty, paclet:ref/EntityProperty])  ▪  (RefLink[EntityValue, paclet:ref/EntityValue])  ▪  (RefLink[Function, paclet:ref/Function])  ▪  (RefLink[Association, paclet:ref/Association])  ▪  (RefLink[ExtendedEntityClass, paclet:ref/ExtendedEntityClass])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Knowledge Representation & Access, paclet:guide/KnowledgeRepresentationAndAccess])}, {▪(RefLinkPlain[Database Connectivity, paclet:guide/DatabaseConnectivity])}, {▪(RefLinkPlain[Engineering Data, paclet:guide/EngineeringData])}, {▪(RefLinkPlain[Life Sciences & Medicine: Data & Computation, paclet:guide/LifeSciencesAndMedicineDataAndComputation])}, {▪(RefLinkPlain[Cultural Data, paclet:guide/CulturalData])}, {▪(RefLinkPlain[People & History, paclet:guide/PeopleAndHistory])}, {▪(RefLinkPlain[Transportation Data, paclet:guide/TransportationData])}, {▪(RefLinkPlain[Working with Information in Relational Databases, paclet:guide/WorkingWithRelationalDatabases])}, {▪(RefLinkPlain[Internet and Computer Systems Data, paclet:guide/InternetAndWebRelatedData])}, {▪(RefLinkPlain[Physics & Chemistry: Data and Computation, paclet:guide/PhysicsAndChemistryDataAndComputation])}, {▪(RefLinkPlain[Astronomical Computation & Data, paclet:guide/AstronomicalComputationAndData])}})}})